【问题标题】:Occasionally, Android SoundPool used with ScheduledExecutorService stops playing sounds有时,与 ScheduledExecutorService 一起使用的 Android SoundPool 会停止播放声音
【发布时间】:2017-07-09 22:11:32
【问题描述】:

这似乎在我的手机和模拟器上发生了大约 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 似乎不再被执行,因为我没有收到任何新的日志消息。非常感谢任何帮助。

【问题讨论】:

    标签: java android audio soundpool scheduledexecutorservice


    【解决方案1】:

    事实证明,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());
        }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多