I initialize audio

SoundPool sp; int error_sound; int del_sound; int level_complete_sound; int level_complete_123; // Аудио sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); error_sound = sp.load(this, R.raw.error_sound, 1); del_sound = sp.load(this, R.raw.del_sound, 1); level_complete_sound = sp.load(this, R.raw.level_complete_sound, 1); level_complete_123 = sp.load(this, R.raw.level_complete_123, 1); 

and then repeatedly use one other sound when pressed

 sp.play(error_sound, 1, 1, 0, 0, 1); 

Everything works, but occasionally, especially when restarting the activity, the sounds work through one, and an error occurs

 E/AudioMixer: AudioMixer::getTrackName out of available tracks E/AudioFlinger: no more track names available E/AudioFlinger: createTrack_l() initCheck failed -12; no control block? E/AudioTrack: AudioFlinger could not create track, status: -12 E/SoundPool: Error creating AudioTrack 

Audio files of small size and duration, about 3-6 seconds. The error itself, as I understand it, indicates that the track cannot be loaded due to lack of memory. But I do not understand what exactly I am doing wrong. Tell me please.

    1 answer 1

    Understood. Error indicates that the limit on loading tracks is exceeded. The limit is set on the entire system and is equal to if I am not mistaken 32 . When restarting the activity sounds are loaded every time, thereby bringing us closer to the limit of 32 tracks. In order to eliminate this, you must clear the SoundPool .

    During initialization we specify:

     SoundPool sp = null; 

    And clear when restarting:

     @Override protected void onPause() { super.onPause(); // Очищаем SoundPool if (sp != null) { sp.release(); sp = null; } } @Override protected void onResume() { super.onResume(); // Подгружаем аудио при возобновлении if (sp == null) { sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); error_sound = sp.load(this, R.raw.error_sound, 1); del_sound = sp.load(this, R.raw.del_sound, 1); level_complete_sound = sp.load(this, R.raw.level_complete_sound, 1); level_complete_123 = sp.load(this, R.raw.level_complete_123, 1); } }