Android L中的SoundPool.load()回归问题

14
在Android L(最新的开发者预览版-Nexus 5)中,SoundPool.load()方法似乎出现了退化,加载一个小于100kb的样本需要超过5秒的时间,而在之前的版本中使用完全相同的代码可以立即加载样本。我尝试使用OGG或MP3格式均有相同的结果。尝试了不同大小但都小于100kb的文件,似乎40kb或80kb没有任何区别,无论是使用OGG还是MP3格式,加载始终会有约5秒的延迟。这似乎是在4.3版本之后又出现了SoundPool的另一个退化。可以轻松地复制该问题。
    pool = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
    // use a listener to start playback after load
    pool.setOnLoadCompleteListener(listener);

    // R.raw.sound1 is either an OGG or MP3 sample under 100kb od size
    int id = pool.load(context, R.raw.sound1, 1);

    // onLoadComplete() method of the listener is called several seconds after the call to laod()

使用在API 21引入的Builders API构建SoundPool时,同样会发生以下情况:

    AudioAttributes attr = new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build();

    pool = new SoundPool.Builder().setAudioAttributes(attr).setMaxStreams(6).build();

还有人遇到这个问题吗?有人找到了解决办法吗?

非常感谢!


在我的Nexus 7 2012上,使用最终版的棒棒糖构建(LRX21P)也遇到了同样的问题。 - Hartok
Nexus 4也有同样的问题,设备通过空中更新升级到了Android L。尽管安装了Android L的Nexus10没有出现这样的问题。 - Taras
同样的问题,Nexus 5..... 真是鬼扯...¿??¿?¿?¿? 使用官方的Android 5.0.1版本。 - NullPointerException
有没有解决方案?Nexus 10上原本只需要大约2.5秒就能加载的应用程序,现在需要接近8秒 :-(。 - Zippy
1个回答

2

提醒大家,这是一个已知的bug:

我尝试了多线程...它可以正常工作,不会阻塞应用程序!但你需要知道,在所有声音加载完成之前,不能调用Soundpool.load方法。即使你调用已经加载过的声音,也会导致应用程序冻结。我猜测SoundPool类有某种内部同步机制。无论如何,你可以使用这种方法让你的应用程序基本工作。下面是一段代码片段可以帮助你:

 private class loadSFXasync extends AsyncTask<String, Integer, Long> {
     protected Long doInBackground(String... str) {
         int count = str.length();
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             mSoundPool.load(str,1);
             publishProgress((int) ((i / (float) count) * 100));
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }

     protected void onPostExecute(Long result) {
         mAllSoundsAreLoaded=true;
     }
 }

在你的代码中:
playSound(String str){
    if (!mAllSoundsAreLoaded)
    return;
    // otherwise: do mSoundPool.load(....)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接