我的 SoundManager 类使用 SoundPool 作为它的音频。我的声音会循环播放,开 1 秒,关 2 秒,直到被告知停止。问题是这将执行一次,然后我注意到 logcat 中有一个错误说“SoundPool:创建 AudioTrack 时出错”,然后是“AudioTrack:AudioFlinger 无法创建轨道,状态:-12”我的代码如下。知道为什么我会收到此错误吗?
public class SoundManager {
public SoundManager(Context c) {
mContext = c;
sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
spSound = sp.load(mContext, R.raw.tone4402, 1);
}
public void Tone3(int timer) {
(new Thread(t3)).start();
}
final Runnable t3 = new Runnable() {
public void run() {
if(isPlaying == false) {
isPlaying = true;
Tone3Run();
}
}
};
public static void Tone3Run() {
//1 sec on, 2 sec off, repeat for 2 minute
Log.d("Sound", "Tone3 Playing");
long startTime = System.currentTimeMillis();
long currentTime;
do
{
currentTime = System.currentTimeMillis();
Log.e("Sound Manager", "Time:" + (currentTime - startTime));
temp = sp.play(spSound, currentVolume, currentVolume, 1, -1, (float)1.0);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sp.pause(temp);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while( ((currentTime - startTime) < 120000) && (boolStop = false) );
sp.release();
sp = new SoundPool(1, 4, 0);
spSound = sp.load(mContext, R.raw.tone4402, 1);
isPlaying = false;
}