1

I'm trying to play an audio Stream using audioTrack, so i have created audioTrack like this:

  public AudioTrack create() {
    if (mTrack != null) {
        if (mTrack.getState() == AudioTrack.STATE_INITIALIZED) {
            return mTrack;
        } else {
            mTrack.release();
            mTrack = null;
        }
    }

    mBuffSize = AudioTrack.getMinBufferSize(Settings.SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
    mTrack = new AudioTrack(AudioManager.STREAM_MUSIC, Settings.SAMPLE_RATE,
            AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, mBuffSize, AudioTrack.MODE_STREAM);
    if (mTrack.getState() == AudioTrack.STATE_INITIALIZED) {
        mTrack.play();
        return mTrack;
    }
    return null;
}

when i finis i call stop like this:

public void stop() {
    if (mTrack != null) {
        if (mTrack.getState() == AudioTrack.STATE_INITIALIZED) {
            mTrack.release();
            mTrack = null;
        }

    }

}

in some device, When i frequently create and stop, i get this:

AudioFlinger could not create track, status: -12.

I searched this issue in StackOverflow many times, but all the answers talk about soundpool , and i have not used a soundpool

Any help would be greatly appreciated!(i'm a Chinese and my English is not good)

Braiam
  • 1
  • 11
  • 47
  • 78
Zenan.Lai
  • 11
  • 4
  • Don't post screenshots of your code..paste your code snippets directly – Akshay Sep 15 '17 at 08:41
  • Thanks !but i dont know how to do this, so how about this? – Zenan.Lai Sep 15 '17 at 08:49
  • Yeah..better now.. – Akshay Sep 15 '17 at 09:11
  • Have you added permissions in your manifest? – Akshay Sep 15 '17 at 09:12
  • witch permission i need to add? i'm not sure,but it runing nomally in other divece – Zenan.Lai Sep 15 '17 at 09:24
  • It sounds like you have short sound clips and you are playing them many times quickly. The other answers talk about [`SoundPool`](https://developer.android.com/reference/android/media/SoundPool.html) with this error because that's what you should be using. The native resources behind `AudioTrack` are not released fast enough for your use case. – Dave Sep 15 '17 at 10:25
  • @Dave My Application is running in interphone , it can receive short or long audio stream frequently. once play finished, i call stop(). – Zenan.Lai Sep 16 '17 at 01:06
  • My suggestion would be to reuse a single `AudioTrack` instance for playback. Do not release and recreate the `AudioTrack`. Instead, call `flush`, `pause`, and `play` to make transitions from one source to another. – Dave Sep 16 '17 at 03:09
  • @Dave oh! This is really useful! I just do what you say! thank you very much bro! – Zenan.Lai Sep 16 '17 at 10:11
  • @Zenan.Lai I hope so. If I have helped you devise a solution, I will post it as an answer. Please edit my answer with the working code. – Dave Sep 16 '17 at 14:54
  • @Dave OK,I will. Thank you again! – Zenan.Lai Sep 18 '17 at 01:39

1 Answers1

1

Add

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></uses-permission>

to play the audio stream.

Akshay
  • 1,161
  • 1
  • 12
  • 33