mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-10-01 14:58:14 +00:00
ppsspp on Android gains and loses audio focus
This commit is contained in:
1 parent
36b82e6159
commit
6f95936da9
3 files changed
+73
-12
No files matched your search
@@ -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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user