Add some protection against double init of the Android audio backend
This commit is contained in:
parent
5a167e03cd
commit
29c4f05ed5
1 changed files with 26 additions and 22 deletions
|
@ -490,17 +490,19 @@ public class SDLActivity extends Activity {
|
||||||
// latency already
|
// latency already
|
||||||
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
|
desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
|
||||||
|
|
||||||
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
|
if (mAudioTrack == null) {
|
||||||
channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
|
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
|
// Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
|
||||||
// Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
|
// 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");
|
if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
|
||||||
mAudioTrack = null;
|
Log.e("SDL", "Failed during initialization of Audio Track");
|
||||||
return -1;
|
mAudioTrack = null;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
audioStartThread();
|
audioStartThread();
|
||||||
|
@ -511,17 +513,19 @@ public class SDLActivity extends Activity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void audioStartThread() {
|
public static void audioStartThread() {
|
||||||
mAudioThread = new Thread(new Runnable() {
|
if (mAudioThread == null) {
|
||||||
@Override
|
mAudioThread = new Thread(new Runnable() {
|
||||||
public void run() {
|
@Override
|
||||||
mAudioTrack.play();
|
public void run() {
|
||||||
nativeRunAudioThread();
|
mAudioTrack.play();
|
||||||
}
|
nativeRunAudioThread();
|
||||||
});
|
}
|
||||||
|
});
|
||||||
// I'd take REALTIME if I could get it!
|
|
||||||
mAudioThread.setPriority(Thread.MAX_PRIORITY);
|
// I'd take REALTIME if I could get it!
|
||||||
mAudioThread.start();
|
mAudioThread.setPriority(Thread.MAX_PRIORITY);
|
||||||
|
mAudioThread.start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void audioWriteShortBuffer(short[] buffer) {
|
public static void audioWriteShortBuffer(short[] buffer) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue