Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Add some protection against double init of the Android audio backend

  • Loading branch information
gabomdq committed Aug 8, 2013
1 parent e0f5dfd commit f3fc77450935629d6afd2449f9a52d8ca2e54d9e
Showing with 26 additions and 22 deletions.
  1. +26 −22 android-project/src/org/libsdl/app/SDLActivity.java
@@ -490,17 +490,19 @@ public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, i
// latency already
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);

mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);

// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
// Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()

if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Log.e("SDL", "Failed during initialization of Audio Track");
mAudioTrack = null;
return -1;
if (mAudioTrack == null) {
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);

// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
// Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()

if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
Log.e("SDL", "Failed during initialization of Audio Track");
mAudioTrack = null;
return -1;
}
}

audioStartThread();
@@ -511,17 +513,19 @@ public static int audioInit(int sampleRate, boolean is16Bit, boolean isStereo, i
}

public static void audioStartThread() {
mAudioThread = new Thread(new Runnable() {
@Override
public void run() {
mAudioTrack.play();
nativeRunAudioThread();
}
});

// I'd take REALTIME if I could get it!
mAudioThread.setPriority(Thread.MAX_PRIORITY);
mAudioThread.start();
if (mAudioThread == null) {
mAudioThread = new Thread(new Runnable() {
@Override
public void run() {
mAudioTrack.play();
nativeRunAudioThread();
}
});

// I'd take REALTIME if I could get it!
mAudioThread.setPriority(Thread.MAX_PRIORITY);
mAudioThread.start();
}
}

public static void audioWriteShortBuffer(short[] buffer) {

0 comments on commit f3fc774

Please sign in to comment.