Android L SoundPool.load() regression

匆匆过客 提交于 2019-12-02 19:25:11

Just so you guys know, this is a known bug:

I have tried multi-threading... It works ok in a sense that it doesn't block the app! But you need to know that you can't call Soundpool.load method before all of your sounds are loaded. Even if you call load on a sound that has already been loaded, it causes the app to freeze. I guess the SoundPool class has some sort of internal synchronization of some sort. Anyways, you can make your app sort of work using this method. This is sort of an snippet that can help:

 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;
     }
 }

and in your code:

playSound(String str){
    if (!mAllSoundsAreLoaded)
    return;
    // otherwise: do mSoundPool.load(....)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!