android - SoundPool 停止播放

标签 android soundpool

我有一个按钮,按下时会通过 SoundPool 发出短促的音效。该文件是 16k mp3。
声音效果适用于数百次按钮点击,但在某些时候我收到此错误(并且声音不再针对点击播放):

E/AudioFlinger(   34): no more track names available
E/AudioTrack(  904): AudioFlinger could not create track, status: -12
E/SoundPool(  904): Error creating AudioTrack
W/AudioFlinger(   34): write blocked for 81 msecs, 4573 delayed writes, thread
xb3f0

创建时:

setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundpool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        // TODO Auto-generated method stub
        loaded = true;
    }
});

//save the effect somewhere:
MyUtil.regularSound = soundpool.load(this, R.raw.regular_click,1);

单击时:

AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVol = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVol = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float vol = actualVol/maxVol;
if (loaded) {
    soundpool.play(MyUtil.regularSound,vol,vol,1,0,1f);
}

在开始另一个 Intent 之前:

Intent i = new Intent(con, EndScreen.class);
if (soundpool != null) {
    soundpool.release();
    soundpool = null;
}
startActivity(i);

最佳答案

我刚刚花了几个小时来解决同样的问题。

-12 错误意味着分配给应用程序的声音内存不足。使用 .finish() 不会释放 SoundPool 的内存。您必须使用.release()

在我的应用程序中,我在 onclick 方法上有一个 soundPool.release() 来启动下一个 Activity ,

更改声音格式对我来说也不起作用。

关于android - SoundPool 停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566626/

相关文章:

android - 如何在 Jetpack Compose 中删除 Canvas 时变得透明,现在我变成了白色?

.net - 在 .NET 中将二进制 XML 读取为文本

java - 如何从 FirestorePagingAdapter 中的 onLoadingStateChanged 方法隐藏/显示进度条?

android - Grade Plugin 3-alpha1 outputFile 导致错误

java - 有时,与ScheduledExecutorService一起使用的Android SoundPool会停止播放声音

android - Alarmmanager和Soundpool-Android

java - Android:使用 SSL 交换消息时出现问题

android - 声音 fragment 的堆大小溢出问题

android - 使用 SoundPool 播放声音

android - 在 Android 上将循环设置为 -1 时不播放声音