Android更新Visualizer音频会话ID

时间:2014-10-20 17:22:44

标签: android audio android-mediaplayer

我有一个从MediaPlayer类播放音频文件的应用程序。

我已将Visualizer附加到当前的音频会话ID,这可以正常工作。

当我更改音轨,从而更改音频会话ID时,我需要重新创建Visualizer,因为我找不到设置新音频会话ID的方法。

这会产生一个问题,即新的VisualizerView会添加到我的布局中。如何更新当前的音频会话ID,或者删除VisualizerView的当前实例并添加新的实例?

代码:

public class PlaySongActivity extends Activity{
    private static final float VISUALIZER_HEIGHT_DIP = 50f;
    private Visualizer mVisualizer;
    private VisualizerView mVisualizerView;
    private LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_song);

        mLinearLayout = (LinearLayout) findViewById(R.id.LayoutViewVisualizer);

        playSong();
    } 

    /* Below methods are used with buttons to play/play next/play previous song */

    private void playSong(Bundle extras) {
        //playing music from a service
        service.playMusic();
        setupVisualizerFx();
        mVisualizer.setEnabled(true);
    }

    private void playPreviousSong() {
        service.playPrevious();
        setupVisualizerFx();
        mVisualizer.setEnabled(true);
    }

    private void playNextSong() {
        service.playNext();
        setupVisualizerFx();
        mVisualizer.setEnabled(true);
    }

/* 
 * Below code that is used for the Visualizer is taken from: 
 * http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/media/AudioFxDemo.html
 */

private void setupVisualizerFx() {        
    // Create a VisualizerView (defined below), which will render the simplified audio
    // wave form to a Canvas.
    mVisualizerView = new VisualizerView(this);
    mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            (int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
    mLinearLayout.addView(mVisualizerView);
    // Create the Visualizer object and attach it to 
    int audioSessionId = service.getAudioSessionId();
    mVisualizer = new Visualizer(audioSessionId);
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) {
            mVisualizerView.updateVisualizer(bytes);
        }

        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
    }, Visualizer.getMaxCaptureRate() / 2, true, false);
}

/**
 * A simple class that draws waveform data received from a
 * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
 */
class VisualizerView extends View {
    private byte[] mBytes;
    private float[] mPoints;
    private Rect mRect = new Rect();

    private Paint mForePaint = new Paint();

    public VisualizerView(Context context) {
        super(context);
        init();
    }

    private void init() {
        mBytes = null;

        mForePaint.setStrokeWidth(1f);
        mForePaint.setAntiAlias(true);
        mForePaint.setColor(getResources().getColor(R.color.green));
    }

    public void updateVisualizer(byte[] bytes) {
        mBytes = bytes;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBytes == null) {
            return;
        }

        if (mPoints == null || mPoints.length < mBytes.length * 4) {
            mPoints = new float[mBytes.length * 4];
        }

        mRect.set(0, 0, getWidth(), getHeight());

        for (int i = 0; i < mBytes.length - 1; i++) {
            mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
            mPoints[i * 4 + 1] = mRect.height() / 2
                    + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
            mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
            mPoints[i * 4 + 3] = mRect.height() / 2
                    + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
        }

        canvas.drawLines(mPoints, mForePaint);
    }
}

}

这是我的代码在调用playNext()或playPrevious()时的当前结果:

result

正如您所看到的,VisualizerViews彼此堆积在一起。我希望新的VisualizerView替换旧的VisualizerView,或者更新当前的Visual SessionView的音频会话ID(如果可能的话)。非常感谢任何帮助。

马库斯

1 个答案:

答案 0 :(得分:1)

通过在setupVisualizerFx()方法中添加以下代码行解决了这个问题:

if(mVisualizer != null)
        mVisualizer.setEnabled(false);

现在该方法如下:

private void setupVisualizerFx() {      
    if(mVisualizer != null)
        mVisualizer.setEnabled(false);
    // Create the Visualizer object and attach it to 
    int audioSessionId = service.getAudioSessionId();
    mVisualizer = new Visualizer(audioSessionId);

    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) {
            mVisualizerView.updateVisualizer(bytes);
        }

        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
    }, Visualizer.getMaxCaptureRate() / 2, true, false);
}

编辑:某些代码已转移到onCreate方法