Pet*_*vka 14 android soundpool android-5.0-lollipop
在Android L上 - 最新的开发人员预览版(Nexus 5),SoundPool.load()方法似乎有一个回归,加载样本(<100kb)需要> 5秒,其中样本加载到pre-L系统上立即使用相同的代码.
我尝试过OGG或MP3,两者都有相同的结果.尝试了不同的尺寸但都低于100kb.似乎40kb或80kb没有任何区别,OGG或MP3也是如此.加载总是大约5s延迟.
这似乎是在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()
Run Code Online (Sandbox Code Playgroud)
使用Builders引入的API 21构建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();
Run Code Online (Sandbox Code Playgroud)
还有其他人遇到过这种情况吗?有人找到了解决方法吗?
非常感谢!
让你们知道,这是一个已知的错误:
我尝试过多线程...从某种意义上说它工作正常,它不会阻止应用程序!但您需要知道,在加载所有声音之前,您不能调用 Soundpool.load 方法。即使您对已加载的声音调用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
并在您的代码中:
playSound(String str){
if (!mAllSoundsAreLoaded)
return;
// otherwise: do mSoundPool.load(....)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2187 次 |
| 最近记录: |