diff --git a/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java b/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java new file mode 100644 index 0000000000..c3dbfb7d9f --- /dev/null +++ b/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java @@ -0,0 +1,25 @@ +package com.henrikrydgard.libnative; +import android.media.AudioManager; +import android.media.AudioManager.OnAudioFocusChangeListener; + +public class AudioFocusChangeListener implements OnAudioFocusChangeListener{ + //not used right now, but we may need to use it sometime. So just store it + //for now. + private boolean hasAudioFocus = false; + + @Override + public void onAudioFocusChange(int focusChange) { + switch (focusChange){ + case AudioManager.AUDIOFOCUS_GAIN: + hasAudioFocus = true; + break; + + case AudioManager.AUDIOFOCUS_LOSS: + hasAudioFocus = false; + } + + // TODO Auto-generated method stub + + } + +} diff --git a/android/src/com/henrikrydgard/libnative/NativeActivity.java b/android/src/com/henrikrydgard/libnative/NativeActivity.java index 0afcd398b5..4f608ff7b8 100644 --- a/android/src/com/henrikrydgard/libnative/NativeActivity.java +++ b/android/src/com/henrikrydgard/libnative/NativeActivity.java @@ -106,12 +106,15 @@ public class NativeActivity extends Activity { private int optimalFramesPerBuffer; private int optimalSampleRate; + //audioFocusChangeListener to listen to changes in audio state + private AudioFocusChangeListener audioFocusChangeListener; + private AudioManager audioManager; + @TargetApi(17) private void detectOptimalAudioSettings() { - AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); try { - optimalFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)); - optimalSampleRate = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); + optimalFramesPerBuffer = Integer.parseInt(this.audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)); + optimalSampleRate = Integer.parseInt(this.audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); } catch (NumberFormatException e) { // Ignore, if we can't parse it it's bogus and zero is a fine value (means we couldn't detect it). } @@ -153,6 +156,12 @@ public class NativeActivity extends Activity { } public void Initialize() { + + //initialise audio classes. Do this here since detectOptimalAudioSettings() + //needs audioManager + this.audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); + this.audioFocusChangeListener = new AudioFocusChangeListener(); + if (Build.VERSION.SDK_INT >= 9) { // Native OpenSL is available. Let's use it! useOpenSL = true; @@ -214,7 +223,8 @@ public class NativeActivity extends Activity { } else { Log.i(TAG, "OpenGL ES 2.0 detected."); } - + + /* editText = new EditText(this); editText.setText("Hello world"); @@ -239,9 +249,13 @@ public class NativeActivity extends Activity { Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setVolumeControlStream(AudioManager.STREAM_MUSIC); - - if (!useOpenSL) - audioPlayer = new NativeAudioPlayer(); + + + //intialize audio and tell PPSSPP to gain audio focus + if (!useOpenSL){ + audioPlayer = new NativeAudioPlayer(); + } + NativeAudioPlayer.gainAudioFocus(this.audioManager, this.audioFocusChangeListener); NativeApp.audioInit(); Point size = new Point(); @@ -277,6 +291,9 @@ public class NativeActivity extends Activity { NativeApp.audioShutdown(); audioPlayer = null; mGLSurfaceView = null; + audioFocusChangeListener = null; + audioManager = null; + } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @@ -294,6 +311,8 @@ public class NativeActivity extends Activity { protected void onPause() { super.onPause(); Log.i(TAG, "onPause"); + + NativeAudioPlayer.loseAudioFocus(this.audioManager, this.audioFocusChangeListener); if (audioPlayer != null) { audioPlayer.stop(); } @@ -313,7 +332,9 @@ public class NativeActivity extends Activity { } else { Log.e(TAG, "mGLSurfaceView really shouldn't be null in onResume"); } - if (audioPlayer != null) { + + NativeAudioPlayer.gainAudioFocus(this.audioManager, this.audioFocusChangeListener); + if (audioPlayer != null) { audioPlayer.play(); } NativeApp.resume(); diff --git a/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java b/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java index 2d98e25f08..34e7b94cdc 100644 --- a/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java +++ b/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java @@ -2,23 +2,24 @@ package com.henrikrydgard.libnative; import android.media.AudioFormat; import android.media.AudioManager; +import android.media.AudioManager.OnAudioFocusChangeListener; import android.media.AudioTrack; import android.util.Log; - +import com.henrikrydgard.libnative.AudioFocusChangeListener; public class NativeAudioPlayer { private String TAG = "NativeAudioPlayer"; private Thread thread; private boolean playing_; - public NativeAudioPlayer() { - } + // Calling stop() is allowed at any time, whether stopped or not. // If playing, blocks until not. public synchronized void stop() { if (thread != null) { waitUntilDone(); + } else { Log.e(TAG, "Was already stopped"); } @@ -32,7 +33,21 @@ public class NativeAudioPlayer { Log.e(TAG, "Was already playing"); } } - + + //keep this static so we can call this even if we don't + //instantiate NativeAudioPlayer + public static void gainAudioFocus(AudioManager audioManager,AudioFocusChangeListener focusChangeListener) { + audioManager.requestAudioFocus(focusChangeListener, + AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); + } + + //keep this static so we can call this even if we don't + //instantiate NativeAudioPlayer + public static void loseAudioFocus(AudioManager audioManager,AudioFocusChangeListener focusChangeListener){ + audioManager.abandonAudioFocus(focusChangeListener); + } + + private void playStreaming() { playing_ = true; thread = new Thread(new Runnable() {