[Android] AudioService: implement audio focus if supported

Edward Wang git at videolan.org
Wed Sep 12 02:35:48 CEST 2012


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Tue Sep 11 18:40:50 2012 -0400| [88b9e199635a0eb7d617345342f5545a1efd0783] | committer: Edward Wang

AudioService: implement audio focus if supported

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=88b9e199635a0eb7d617345342f5545a1efd0783
---

 vlc-android/src/org/videolan/vlc/AudioService.java |   39 ++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index b948ac8..32742f2 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -35,6 +35,7 @@ import org.videolan.vlc.interfaces.IAudioService;
 import org.videolan.vlc.interfaces.IAudioServiceCallback;
 
 import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
 import android.app.Notification;
 import android.app.PendingIntent;
 import android.app.Service;
@@ -47,6 +48,7 @@ import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.media.AudioManager;
+import android.media.AudioManager.OnAudioFocusChangeListener;
 import android.net.Uri;
 import android.os.Handler;
 import android.os.IBinder;
@@ -142,6 +144,41 @@ public class AudioService extends Service {
         return mInterface;
     }
 
+    @TargetApi(8)
+    private void changeAudioFocus(boolean gain) {
+        if(!Util.isGingerbreadOrLater()) // NOP if not supported
+            return;
+
+        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
+        if(gain)
+            am.requestAudioFocus(audioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
+        else
+            am.abandonAudioFocus(audioFocusListener);
+    }
+
+    private final OnAudioFocusChangeListener audioFocusListener = new OnAudioFocusChangeListener() {
+        int volume = -1;
+        @Override
+        public void onAudioFocusChange(int focusChange) {
+            AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
+            if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK ||
+               focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
+                volume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
+                int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
+                /*
+                 * Lower the volume to 19% to "duck" when an alert or something
+                 * needs to be played.
+                 */
+                am.setStreamVolume(AudioManager.STREAM_MUSIC, (int)(0.19*maxVol), 0);
+            } else {
+                if(volume != -1) {
+                    am.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
+                    volume = -1;
+                }
+            }
+        }
+    };
+
     private final BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -282,6 +319,7 @@ public class AudioService extends Service {
             switch (msg.getData().getInt("event")) {
                 case EventManager.MediaPlayerPlaying:
                     Log.i(TAG, "MediaPlayerPlaying");
+                    service.changeAudioFocus(true);
                     break;
                 case EventManager.MediaPlayerPaused:
                     Log.i(TAG, "MediaPlayerPaused");
@@ -408,6 +446,7 @@ public class AudioService extends Service {
         mHandler.removeMessages(SHOW_PROGRESS);
         hideNotification();
         executeUpdate();
+        changeAudioFocus(false);
     }
 
     private void next() {



More information about the Android mailing list