Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Add some protection against double init of the Android audio backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gabomdq committed Aug 8, 2013
1 parent e0f5dfd commit f3fc774
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit f3fc774

Please sign in to comment.