这似乎在我的手机和模拟器上发生了大约15次。 SoundPool将播放声音约3秒然后停止。当我正在播放声音时,我反复收到此消息,我理解这只是意味着无法使用低延迟模式。当声音停止时,不再产生这些警告。
W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
如果我看整个手机的Android显示器,我总是在声音停止时得到这个,而不是其他。没有例外被抛出。
07-09 22:14:15.630 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7935
07-09 22:14:15.630 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7928
07-09 22:14:15.750 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7936
07-09 22:14:15.750 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7937
07-09 22:14:15.870 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7938
07-09 22:14:16.130 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7939
07-09 22:14:16.130 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7940
07-09 22:14:16.250 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7941
07-09 22:14:16.390 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7943
07-09 22:14:16.390 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7944
07-09 22:14:17.620 2601-2890/? I/AudioPolicyManager: stopOutput() output 2, stream 7, session 7942
07-09 22:14:17.620 2601-2890/? I/AudioPolicyManager: getNewOutputDevice() selected device 0
07-09 22:14:17.620 2601-2890/? I/AudioPolicyManager: setOutputDevice() output 2 device 0x0000 delayMs 40
07-09 22:14:17.620 2601-2890/? I/AudioPolicyManager: setOutputDevice() prevDevice 0x0002
07-09 22:14:17.620 2601-2890/? I/AudioPolicyManager: setOutputDevice() setting same device 0x0000 or null device for output 2
这就是我创建SoundPool的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
clips = new SoundPool.Builder()
.setMaxStreams(22)
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
.build())
.build();
} else {
clips = new SoundPool(22, AudioManager.STREAM_ALARM, 0);
}
我的其余代码太大而无法发布,但我正在加载和播放剪辑,如下所示:
ScheduledExecutorService trigger = Executors.newScheduledThreadPool(3);
int e1 = clips.load(this, noteFiles.getResourceId(0,0), 1);
int[] noteIDs = {e1};
playNote = new Runnable() {
public void run() {
clips.play(noteIDs[0], realTuneVol, realTuneVol, 1, 0, 1);
}
}
clips.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
if (savedInstanceState != null) {
trigger.shutdown();
} else {
trigger.scheduleAtFixedRate(playNote, 0, 125, TimeUnit.MILLISECONDS);
}
}
}
}
});
我正在同时播放很多片段,所以我理解这可能是个问题,但是我没有注意到播放的片段数和这个错误之间有很多相关性。然而,我所阅读和体验的是,如果我尝试播放太多剪辑,SoundPool将开始跳过它们,但它不会一起停止。所以我想知道这个问题是否与我尝试播放的片段数量无关,并且实际上与ScheduledExecutorService相关。当声音停止时,Runnable似乎不再执行,因为我没有收到任何新的Log消息。非常感谢任何帮助。
答案 0 :(得分:0)
事实证明,ScheduledExecutorService会抑制异常,如此处所述。 http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/
因此,通过将整个Runnable包装在try catch块中,我能够发现有时因为程序的另一部分而发生了ArrayIndexOutOfBoundsException,从而导致了问题。
playNote = new Runnable() {
public void run() {
try{
clips.play(noteIDs[0], realTuneVol, realTuneVol, 1, 0, 1);
}
catch (Throwable th){
Log.v("ErrorInRunnable", th.toString());
}
}
}