gpt4 book ai didi

android - Android更新Visualizer Audio Session ID

转载 作者:行者123 更新时间:2023-12-03 02:28:41 24 4
gpt4 key购买 nike

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

我已将Visualizer附加到当前的音频 session ID,效果很好。

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

这就产生了一个新的VisualizerView被添加到我的布局的问题。如何更新当前的音频 session 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()时的当前结果:

如您所见,VisualizerView彼此堆积如山。我希望新的VisualizerView替换旧的VisualizerView,或者如果可能的话,更新当前的音频 session ID。任何帮助是极大的赞赏。

马库斯

最佳答案

通过在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方法

关于android - Android更新Visualizer Audio Session ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26471348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com