19

I am having a problem With SoundPool as it refuses to work with my .ogg files. I am getting this error:

AudioFlinger could not create track, status: -12
 Error creating AudioTrack

I've found a thread concerning this and the (possible) answer is:

Make sure you use .ogg media files with constant bitrate!

Is this the case? If yes - which application to use (Audacity doesn't support .ogg custom export settings). If not - what else could be wrong?

As a side note - before I used MediaPlayer but now I want to play a few sounds parallel.

Braiam
  • 1
  • 11
  • 47
  • 78
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
  • I was getting this error if using an ogg OR mp3 and setting the loop arg in play() to anything but 0 - using wav seems to sort it – Dori Oct 12 '12 at 16:31

7 Answers7

17

MAJOR EDIT: FOUND A WAY TO FIX YOUR DAY!! SCROLL DOWN BELOW!!

I'm in the same boat with you. Before reading, I must let you know that I failed to provide an answer that can fix this problem.

I have seen many, many Stack Overflow topics regarding SoundPool, but I haven't seen a lot of SoundPool problems with this in regard with Java code from an Android book:

AudioTrack: AudioFlinger could not create track, status -12.
SoundPool: Error creating AudioTrack.

Here's the code snipped from Beginning Android 4 Game Development source code provided here (see below). You get the explosion.ogg file there (in the trunk, find the /asset folder of Chapter 4 - Android Basics). You have to click on "View raw file" on your right to obtain the file.

All I did was I copied the source code directly into Eclipse, and added the explosion.ogg file provided from the trunk, into my project. I executed Android emulator, tried playing the file, and I can tell you I still get the same Logcat errors quoted above.

A person provided a video on Youtube stating that SoundPool works normally. Video link is given below. I did what the video instructed me to code, I grabbed the same explosion.ogg file I got from the second link provided above. I still get these errors:

03-13 05:17:10.143: E/AudioTrack(3370): AudioFlinger could not create track, status: -12
03-13 05:17:10.143: E/SoundPool(3370): Error creating AudioTrack

I do know that SoundPool exists since Android 1.0, at API level 1. There shouldn't be any reason for SoundPool to fail to work, even when I have proof that SoundPool works (the video link, 2nd one).

These are my findings and may or may not help others in a good way. In short, you're not alone here.

Source:

  1. http://code.google.com/p/beginning-android-games/source/browse/trunk/ch04-android-basics/src/com/badlogic/androidgames/SoundPoolTest.java
  2. http://www.youtube.com/watch?v=ioGWpu8Ud7A

EDIT: I am able to play sounds from SoundPool.

Here's my code:

package nttu.edu.test;

import nttu.edu.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SoundPoolTest extends Activity implements OnClickListener {
    SoundPool soundPool = null;
    int explosionId = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView v = new TextView(this);
        v.setOnClickListener(this);
        setContentView(v);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        v.setText("Click anywhere to play the sound.");
    }

    protected void onResume() {
        super.onResume();
        if (soundPool == null) {
            soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
            explosionId = soundPool.load(this, R.raw.explosion, 1);
        }
    }

    protected void onPause() {
        super.onPause();
        if (soundPool != null) {
            soundPool.release();
            soundPool = null;
        }
    }

    public void onClick(View v) {
        if (explosionId != 0)
            soundPool.play(explosionId, 1, 1, 0, 0, 1);
    }
}

What I did was refactor my code around. I learned this tip from one of the comments in the Youtube video link, where the author of the comment goes by the name of "ErichLancaster". All credits go to him. I hoped this helped you out.

I also set the Android emulator resolution size to QVGA, if it really matters or not.

tom_mai78101
  • 2,383
  • 2
  • 32
  • 59
  • 1
    I also refactored my code (a few times) and now it works! How can this be? Maybe the developers should get contacted about SoundPool class, but I have no explicit info on what is wrong. – c0dehunter Mar 15 '12 at 20:13
  • There is one little problem still persisting though - during play, I can see this errors stacking up in Logcat: `Heap size overflow! req size: 1049280, max size: 1048576` Why? – c0dehunter Mar 15 '12 at 20:14
  • @PrimožKralj As for the Logcat errors, I don't know how to fix it. My simple troubleshooting would be to increase the heap size in the emulator. If that doesn't work, then the only option left is to get in contact with one of the Android SDK dev, and let the dev know about this. – tom_mai78101 Mar 16 '12 at 09:47
  • When we refactor the codes around, we never actually refactor them around the workflow of how Activity works. The only explict info I can grasp, is that SoundPool class needs to be "reloaded", or "recreated" in order to work. When an Activity is paused, I guessed that the Activity wouldn't be able to keep important parts of the memory in. That's just my two cents. – tom_mai78101 Mar 16 '12 at 09:52
  • Will report as soon as I get to my workstation (next week). You have provided me with a lot of info and shared your research, so you definatelly deserve the bounty :) – c0dehunter Mar 17 '12 at 13:40
  • 1
    hello, thanks for your code, anyway I realised that the length or the size of the file matter, in my first case the length was 10 seconds and it won't work, but when I trimmed it to 1 second now its working. – Mohammad Ersan Feb 09 '13 at 01:48
  • I spent 8 hours until I found this solution. Every other answer does not work. Thank you and thanks ErichLancaster – user3078406 May 18 '20 at 12:19
3

For me status -12 was caused by specifying too many channels when I instantiate SoundPool. So it was probably just running out of memory trying to keep too many channels around.

cutopia
  • 31
  • 1
2

I was facing this error too recently. Mine was a collision actually taking place multiple times in a loop so it would actually stack the sounds until they dropped out, this causing the error, yet it would keep playing. I fixed this by registering a bounce as 1 when it occurs.

 // bouncecount=
 // 0=no bounce
 // 1=set when bounce occurs and plays the sound
 // 21=reset

Since the game runs at roughly 60fps, this only allows one instance of the sound 3 times a second and could be adjusted easily by changing the 20. Not only did this reduce jitter in the game, it isn't the least bit noticeable.

if(bouncecount==1){
   sounds.play(sbump,1,1, 0, 0, 1);bouncecount++;}
else{if(bouncecount<20&&bouncecount!=0){bouncecount++;}
     else{bouncecount=0;}}

Hope this helps someone.

a54studio
  • 965
  • 11
  • 11
2

I had to play around 10 Sounds in parallel for a Drumkit-Simulation. Therefore, I put every sound in an own soundpool and used StreamIds to identify and stop any equal sound, in case it was already playing.

int newStreamID = soundPool.play(soundIDs.get(0), volume, volume, 1, 0, randomFloatRate);
if (StreamID != null) {
    soundPool.stop(StreamID);
}
StreamID = newStreamID;

As far as I know Soundpool decompresses the audiofiles, so you can save CPU-Usage by using the .wav format.

For changing Audio formats I use FormatFactory

Markus Weber
  • 1,059
  • 1
  • 11
  • 24
0

There is a problem with looping: when I set repeat to -1 I get this error, but with 0 everything is working properly. I've noticed that some sounds give this error when I'm trying to play them one by one. For example:

mSoundPool.stop(mStreamID);
mStreamID = mSoundPool.play(mRandID, mVolume, mVolume, 1, -1, 1f);

In such case, first track is played ok, but when I switch sounds, next track gives this error. It seems that using looping, a buffer is somehow overloaded, and mSoundPool.stop cannot release resources immediately.

Solution:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
   @Override
   public void run() {
      mStreamID = mSoundPool.play(mRandID, mVolume, mVolume, 1, -1, 1f);
}, 350);

And it's working, but delay is different for different devices.

Oleksandr Albul
  • 1,611
  • 1
  • 23
  • 31
0

i just had this issue working on my game. I am building it in android native using java. My problem was 2 fold. 1st I used too many channels when creating my soundpool, then I never released the soundpool when the game ended and went to the game over screen. I used onStop method to release both soundpool and audiomanager when the game ended and that fixed the problem for me.

// in GameActivity
    @Override
    protected void onStop() {
        gameView.audioHandler.release();
        super.onStop();
    }
// in AudioHandler class
    public void release() {
        soundPool.release(); // this is my SoundPool
        battleMusic.release(); // this is my AudioManager
    }

Update: The issue still reoccurred after some more testing. However, what finally ended up eliminating the issue completely ended up being a combination of the previous code and then limiting the sound pool to a max stream of 10 and lowering the quality of the sound MP3s used. The issue was primarily occurring when I had 10+ enemies on the screen all firing lasers using the same sound. Per the SoundPool documentation, when the max stream is reached it logically will eliminate old streams. Given that the -12 code signifies that the 1 MB limit of soundpool has been reached, the conclusion I came to was that it was overburdened with sound and thus by setting it to a stream limit of 10 it could never reach that 1 mb limit and have to begin cycling out old streams. It is odd since my sound effects are not that long in duration, but still it reached the limit. I hope this helps someone!I have not had this issue again since reducing the streams.

-1

I follow this answer: http://www.andengine.org/forums/gles2/sound-not-playing-t10820.html

The main idea is that SoundManager class has a SoundPool with little memory allowed (in contradistinction to MusicManager), so in "all messages" in logcat "out of memory" can be seen.

I suppose, every ".play()" called for every sound (don't know what about music) - allocates new memory space in the pool, and to avoid that you can do the following:

- use .play() only once, then .seekTo(0), then .resume() (to play from the begining)

- or use .play() only once, then .pause(), then .resume() (for looping, for example)

  • Welcome to Stack Overflow. Please, try to not provide external links, store the most info that you can here on the answer body. Only if really needed, post external links. People here, usually do not trust it, and with reason to. – Paulo Roberto Rosa Jan 27 '14 at 15:37
  • there is no seekto method for soundpool. – Mahmud Ahsan Jun 29 '14 at 14:06