简体   繁体   English

SoundPool停止播放

[英]SoundPool stops playing

I have a button that when pressed sounds a short sound effect with SoundPool. 我有一个按钮,当按下时,SoundPool发出短促的声音效果。 the file is a 16k mp3. 该文件是一个16k的MP3。
The sound effect works for hundreds of button clicks, but at some point i get this error (and the sound isn't playing for clicks anymore): 声音效果适用于数百次按钮点击,但在某些时候我收到此错误(并且声音不再为点击而播放):

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

onCreate: 的onCreate:

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

onClick: 的onClick:

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

before starting another intent: 在开始另一个意图之前

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

I just spent hours with the same problem. 我只是花了好几个小时同样的问题。

The -12 error means that sound allocated to the app is out of memory. -12错误表示分配给应用程序的声音内存不足。 using .finish() doesn't release the memory of SoundPool. 使用.finish()不会释放SoundPool的内存。 You have to use .release() . 你必须使用.release()

In my app I have a soundPool.release() on the onclick method that starts the next activity, 在我的应用程序中,我在onclick方法上有一个soundPool.release(),它启动下一个活动,

Changing the sound formats didn't work for me either. 改变声音格式对我也不起作用。

Im getting the same error. 我得到了同样的错误。 The thing I did was to try to resample the track, convert it to OGG format and change the quality to 0. I did all this with Audacity 我做的是尝试重新采样轨道,将其转换为OGG格式并将质量更改为0.我使用Audacity做了所有这些

You try code it: here 你试试吧:这里

    /*
    private SoundPool mSoundPool;
    private SparseArray<Integer> mSoundPoolMap;
    private AudioManager mAudioManager;
    */
    public static void clear() {
        if (mSoundManager != null) {
            mSoundManager.mSoundPool = null;
            mSoundManager.mAudioManager = null;
            mSoundManager.mSoundPoolMap = null;
        }
        mSoundManager = null;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM