gpt4 book ai didi

java - AudioTrack - IllegalStateException : play() called on uninitialized AudioTrack

转载 作者:行者123 更新时间:2023-12-01 06:01:56 50 4
gpt4 key购买 nike

我有一个带有 AudioTrack 的线程,我在向下按钮上调用它,在向上按钮上我想停止音频。到目前为止,一切都很好。我的应用程序有 Activity A 和 Activity B。 Activity A 是一些菜单,我从其中启动 Activity B,这是主 Activity ,我将其称为 AudioTrack 等等。问题是,一切都工作正常,直到我第三次进入 Activity B,然后当我打电话玩时发生了这种情况。

E/AudioTrack: AudioFlinger could not create track, status: -12
E/AudioTrack-JNI: Error -12 initializing AudioTrack
E/android.media.AudioTrack: Error code -20 when initializing AudioTrack.
java.lang.IllegalStateException: play() called on uninitialized AudioTrack.

第一次和第二次我什至不需要调用play,只需进入Activity B,但是当我想第三次调用play时,发生了这个错误。为什么???

Activity B 看起来像这样

Activity B playTune(){
if(tuneThread == null){
tuneThread = new startSI();
// tuneThread.setTuneFreq(tuneFreq);
tuneThread.start();
}
return this;
}

void setTuneFreq(double tuneFreq) {
this.tuneFreq = tuneFreq;
if(tuneThread != null){
tuneThread.setTuneFreq(tuneFreq);
}
}

void stopTune(){
if(tuneThread != null){
tuneThread.stopTune();
tuneThread = null;
}
}

protected void onPause() {
stopTune();
super.onPause();
}

protected void onStop() {
stopTune();
super.onStop();

这就是我调用和/或停止线程的方式

setTuneFreq(double);

我调用 OnSenzorChanged(事件监听器),因为我需要基于陀螺仪数据的频率。

playTune();

我在按下按钮后ACTION_DOWN后调用OnTouchListener,只要按下按钮,AudioTrack就会生成信号(直到ACTION_UP),然后我调用

stopTune();

AudioTrack 线程看起来像这样

public class startSI extends Thread {

private boolean isRunning;
private double tuneFreqSI;


private int sr = 44100;
private int sibuffsize = AudioTrack.getMinBufferSize(sr,
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
private AudioTrack siaudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sr, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, sibuffsize,
AudioTrack.MODE_STREAM);


@Override
public void run() {

super.run();
isRunning = true;


short samples[] = new short[sibuffsize];
int amp = 32767;
double twopi = 8. * Math.atan(1.);
double ph = 0.0;

try {

siaudioTrack.play();

} catch(IllegalStateException e) {
System.out.println("error play "+e);
}

while (isRunning) {

double fr = tuneFreqSI;
double fLFO = frLFO;
for (int i = 0; i < sibuffsize; i++) {
samples[i] = (short) (amp * Math.sin(ph));
ph += twopi * fr / sr;
}
siaudioTrack.write(samples, 0, sibuffsize);

}
try {
siaudioTrack.stop();
siaudioTrack.release();

}catch(IllegalStateException e) {
System.out.println("error stop "+e);
}

}

public void setTuneFreq(double tuneFreq, double frLFO, double ampLFO) {

this.tuneFreqSI = tuneFreq;
this.frLFO = frLFO;
this.ampLFO = ampLFO;
}


public void stopTune() {
isRunning = false;

try {
this.join();
this.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

即使在第三次打开 Activity B 后,我也需要启动该线程。

我做错了什么?

最佳答案

我认为您的音轨同时被垃圾收集器销毁了。因此,我建议您在 run() 方法开始时使用此行重新初始化 siaudioTrack。

this.siaudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sr, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, sibuffsize, AudioTrack.MODE_STREAM);

关于java - AudioTrack - IllegalStateException : play() called on uninitialized AudioTrack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55692201/

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