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 760396b76c2ce46a
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
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 02:34:21 2013 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 09:47:42 2013 -0300
     1.3 @@ -490,17 +490,19 @@
     1.4          // latency already
     1.5          desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
     1.6          
     1.7 -        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
     1.8 -                channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
     1.9 -        
    1.10 -        // Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
    1.11 -        // Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
    1.12 -        // Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
    1.13 -        
    1.14 -        if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
    1.15 -            Log.e("SDL", "Failed during initialization of Audio Track");
    1.16 -            mAudioTrack = null;
    1.17 -            return -1;
    1.18 +        if (mAudioTrack == null) {
    1.19 +            mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
    1.20 +                    channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
    1.21 +            
    1.22 +            // Instantiating AudioTrack can "succeed" without an exception and the track may still be invalid
    1.23 +            // Ref: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/media/java/android/media/AudioTrack.java
    1.24 +            // Ref: http://developer.android.com/reference/android/media/AudioTrack.html#getState()
    1.25 +            
    1.26 +            if (mAudioTrack.getState() != AudioTrack.STATE_INITIALIZED) {
    1.27 +                Log.e("SDL", "Failed during initialization of Audio Track");
    1.28 +                mAudioTrack = null;
    1.29 +                return -1;
    1.30 +            }
    1.31          }
    1.32          
    1.33          audioStartThread();
    1.34 @@ -511,17 +513,19 @@
    1.35      }
    1.36      
    1.37      public static void audioStartThread() {
    1.38 -        mAudioThread = new Thread(new Runnable() {
    1.39 -            @Override
    1.40 -            public void run() {
    1.41 -                mAudioTrack.play();
    1.42 -                nativeRunAudioThread();
    1.43 -            }
    1.44 -        });
    1.45 -        
    1.46 -        // I'd take REALTIME if I could get it!
    1.47 -        mAudioThread.setPriority(Thread.MAX_PRIORITY);
    1.48 -        mAudioThread.start();
    1.49 +        if (mAudioThread == null) {
    1.50 +            mAudioThread = new Thread(new Runnable() {
    1.51 +                @Override
    1.52 +                public void run() {
    1.53 +                    mAudioTrack.play();
    1.54 +                    nativeRunAudioThread();
    1.55 +                }
    1.56 +            });
    1.57 +            
    1.58 +            // I'd take REALTIME if I could get it!
    1.59 +            mAudioThread.setPriority(Thread.MAX_PRIORITY);
    1.60 +            mAudioThread.start();
    1.61 +        }
    1.62      }
    1.63      
    1.64      public static void audioWriteShortBuffer(short[] buffer) {