Skip to content
Browse files

Audio focus handling

Request and handle audio focus. This is especially important to mute
audio while in a call.
  • Loading branch information...
1 parent 3a18c19 commit ace2a795b663f912e51da269b022488b31d64b9b @grigorig committed Mar 22, 2013
Showing with 27 additions and 1 deletion.
  1. +27 −1 src/com/cyanogenmod/effem/FmRadioService.java
View
28 src/com/cyanogenmod/effem/FmRadioService.java
@@ -20,6 +20,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
+import android.media.AudioManager;
+import android.media.AudioManager.OnAudioFocusChangeListener;
import android.os.*;
import android.util.Log;
import android.widget.Toast;
@@ -36,7 +38,8 @@
import android.content.BroadcastReceiver;
import java.io.IOException;
-public class FmRadioService extends Service {
+public class FmRadioService extends Service
+ implements AudioManager.OnAudioFocusChangeListener {
// various constants
static final String LOG_TAG = "EffemService";
static final int PLAY_NOTIFICATION = 1;
@@ -60,6 +63,7 @@
private Notification.Builder mRadioNotification;
private Notification mNotificationInstance;
private NotificationManager mNotificationManager;
+ private AudioManager mAudioManager;
private FmReceiver.OnScanListener mReceiverScanListener;
private FmReceiver.OnRDSDataFoundListener mReceiverRdsDataFoundListener;
private FmReceiver.OnStartedListener mReceiverStartedListener;
@@ -110,6 +114,7 @@ public void onCreate() {
mFmReceiver = (FmReceiver)getSystemService("fm_receiver");
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
+ mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
prepareNotification();
// listen for headset connection events
@@ -138,6 +143,24 @@ public void onReceive(Context ctx, Intent intent) {
registerReceiver(mHeadsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
+
+ @Override
+ public void onAudioFocusChange(int focus) {
+ switch (focus) {
+ case AudioManager.AUDIOFOCUS_LOSS:
+ updateReceiverState(false);
+ break;
+ case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
+ updatePlayState(false);
+ break;
+ case AudioManager.AUDIOFOCUS_GAIN:
+ updatePlayState(true);
+ break;
+ default:
+ Log.e(LOG_TAG, "unknown audio focus change: " + focus);
+ }
+ }
+
@Override
public void onDestroy() {
unregisterReceiver(mHeadsetReceiver);
@@ -287,6 +310,8 @@ public void run() {
}
});
updateFrequency(mCurrentFrequency, true);
+ mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
+ AudioManager.AUDIOFOCUS_GAIN);
startForeground(PLAY_NOTIFICATION, mNotificationInstance);
} catch (IOException e) {
Log.e(LOG_TAG, e.toString());
@@ -307,6 +332,7 @@ public void run() {
mCallbacks.onReceiverStateChanged(false);
}
});
+ mAudioManager.abandonAudioFocus(this);
stopForeground(true);
//mNotificationManager.cancel(PLAY_NOTIFICATION);
} catch (IOException e) {

0 comments on commit ace2a79

Please sign in to comment.
Something went wrong with that request. Please try again.