Skip to content

Commit

Permalink
根据播放音乐显示不同频谱案例
Browse files Browse the repository at this point in the history
  • Loading branch information
atguiguafu committed Jan 14, 2017
1 parent 14eba97 commit 509fe4b
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Android应用源码音乐实时跳动频谱显示Demo/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.terry.AudioFx"
minSdkVersion 15
targetSdkVersion 22
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.terry.AudioFx"
android:versionCode="1"
android:versionName="1.0" >

<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AudioFxActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.RECORD_AUDIO" ></uses-permission>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.terry.AudioFx;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;

public class AudioFxActivity extends Activity
{

private static final String TAG = "AudioFxActivity";


private MediaPlayer mMediaPlayer;
private Visualizer mVisualizer;

private BaseVisualizerView mBaseVisualizerView;

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);


setContentView(R.layout.main);
mBaseVisualizerView = (BaseVisualizerView) findViewById(R.id.visualizerview);

mMediaPlayer = MediaPlayer.create(this, R.raw.z8806c);




mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
setupVisualizerFxAndUi();
}
});


mMediaPlayer.setLooping(true);

}


/**
* 生成一个VisualizerView对象,使音频频谱的波段能够反映到 VisualizerView上
*/
private void setupVisualizerFxAndUi()
{

int audioSessionid = mMediaPlayer.getAudioSessionId();
System.out.println("audioSessionid=="+audioSessionid);
mVisualizer = new Visualizer(audioSessionid);
// 参数内必须是2的位数
mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
// 设置允许波形表示,并且捕获它
mBaseVisualizerView.setVisualizer(mVisualizer);
mVisualizer.setEnabled(true);
}

@Override
protected void onPause()
{
super.onPause();
if (isFinishing() && mMediaPlayer != null)
{
mVisualizer.release();
mMediaPlayer.release();
mMediaPlayer = null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package com.terry.AudioFx;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.media.audiofx.Visualizer;
import android.util.AttributeSet;
import android.view.View;

public class BaseVisualizerView extends View implements Visualizer.OnDataCaptureListener{

private static final int DN_W = 480;
private static final int DN_H = 160;
private static final int DN_SL =14;
private static final int DN_SW = 6;

private int hgap = 0;
private int vgap = 0;
private int levelStep = 0;
private float strokeWidth = 0;
private float strokeLength = 0;

/**
* It is the max level.
*/
protected final static int MAX_LEVEL = 13;

/**
* It is the cylinder number.
*/
protected final static int CYLINDER_NUM = 20;

/**
* It is the visualizer.
*/
protected Visualizer mVisualizer = null;

/**
* It is the paint which is used to draw to visual effect.
*/
protected Paint mPaint = null;

/**
* It is the buffer of fft.
*/
protected byte[] mData = new byte[CYLINDER_NUM];

boolean mDataEn = true;
/**
* It constructs the base visualizer view.
* @param context It is the context of the view owner.
*/
public BaseVisualizerView(Context context) {
super(context);

initView();
}

private void initView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.GREEN);
// mPaint.setColor(0xFFd60d25);
mPaint.setStrokeJoin(Join.ROUND);
mPaint.setStrokeCap(Cap.ROUND);
}

public BaseVisualizerView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

float w, h, xr, yr;
w = right - left;
h = bottom - top;
xr = w / (float)DN_W;
yr = h / (float)DN_H;

strokeWidth = DN_SW * yr;
strokeLength = DN_SL * xr;
hgap = (int)((w - strokeLength * CYLINDER_NUM) / (CYLINDER_NUM + 1) );
vgap = (int)(h / (MAX_LEVEL + 2));

mPaint.setStrokeWidth(strokeWidth);
}

protected void drawCylinder(Canvas canvas, float x, byte value) {
if (value < 0) value = 0;
for (int i = 0; i < value; i++) {
float y = getHeight() - i * vgap - vgap;
canvas.drawLine(x, y, x + strokeLength, y, mPaint);
}
}

@Override
public void onDraw(Canvas canvas) {
for (int i = 0; i < CYLINDER_NUM; i ++) {
drawCylinder(canvas, strokeWidth / 2 + hgap + i * (hgap + strokeLength), mData[i]);
}
}

/**
* It sets the visualizer of the view. DO set the viaulizer to null when exit the program.
* @parma visualizer It is the visualizer to set.
*/
public void setVisualizer(Visualizer visualizer) {
if (visualizer != null) {
if (!visualizer.getEnabled()) {
visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[0]);
}
levelStep = 128 / MAX_LEVEL;
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate() / 2, false, true);

} else {

if (mVisualizer != null) {
mVisualizer.setEnabled(false);
mVisualizer.release();
}
}
mVisualizer = visualizer;
}

@Override
public void onFftDataCapture(Visualizer visualizer, byte[] fft,
int samplingRate) {
byte[] model = new byte[fft.length / 2 + 1];
if (mDataEn) {

model[0] = (byte) Math.abs(fft[1]);
int j = 1;
for (int i = 2; i < fft.length;) {
model[j] = (byte) Math.hypot(fft[i], fft[i + 1]);
i += 2;
j++;
}
} else {
for (int i = 0; i < CYLINDER_NUM; i ++) {
model[i] = 0;
}
}
for (int i = 0; i < CYLINDER_NUM; i ++) {
final byte a = (byte)(Math.abs(model[CYLINDER_NUM - i]) / levelStep);

final byte b = mData[i];
if (a > b) {
mData[i] = a;
} else {
if (b > 0) {
mData[i]--;
}
}
}
postInvalidate();
}

@Override
public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform,
int samplingRate) {
// Do nothing.
}

/**
* It enables or disables the data processs.
* @param en If this value is true it enables the data process..
*/
public void enableDataProcess(boolean en) {
mDataEn = en;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<com.terry.AudioFx.BaseVisualizerView
android:id="@+id/visualizerview"
android:layout_width="fill_parent"
android:layout_height="100dp"
/>
</LinearLayout>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AudioFxActivity!</string>
<string name="app_name">android跳动频谱</string>
</resources>

0 comments on commit 509fe4b

Please sign in to comment.