Add some protection against double init of the Android audio backend
authorGabriel Jacobo <gabomdq@gmail.com>
Thu, 08 Aug 2013 09:47:42 -0300
changeset 7603 96b76c2ce46a
parent 7602 9829fb1aeac7
child 7604 aed5e6b271f3
Add some protection against double init of the Android audio backend
android-project/src/org/libsdl/app/SDLActivity.java
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 02:34:21 2013 -0700
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 09:47:42 2013 -0300
@@ -490,17 +490,19 @@
         // 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 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) {