[Android] Don't setup the remote control when the service starts

Ludovic Fauvet git at videolan.org
Mon Nov 5 13:46:30 CET 2012


vlc-ports/android | branch: master | Ludovic Fauvet <etix at videolan.org> | Mon Nov  5 11:52:29 2012 +0100| [3edd097ec9e6e6996ad6e80d38a75cf3964b18c3] | committer: Ludovic Fauvet

Don't setup the remote control when the service starts

This would steal the remote control focus everytime the service is
restarted (for any reason).
The correct behavior is based on the RandomMusicPlayer available in the
SDK samples.

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

 vlc-android/src/org/videolan/vlc/AudioService.java |   70 ++++++++++----------
 .../src/org/videolan/vlc/gui/MainActivity.java     |    2 +-
 .../vlc/gui/audio/AudioPlayerActivity.java         |    2 +-
 3 files changed, 37 insertions(+), 37 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index bbb364a..a5a7d9c 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -87,7 +87,7 @@ public class AudioService extends Service {
     /**
      * RemoteControlClient is for lock screen playback control.
      */
-    private RemoteControlClient mRemoteControlClient = null;
+    private static RemoteControlClient mRemoteControlClient = null;
 
     /**
      * Distinguish between the "fake" (Java-backed) playlist versus the "real"
@@ -135,34 +135,40 @@ public class AudioService extends Service {
             filter = new IntentFilter();
             filter.addAction(Intent.ACTION_MEDIA_BUTTON);
             registerReceiver(new RemoteControlClientReceiver(), filter);
-        } else {
-            setUpRemoteControlClient();
         }
 
         AudioUtil.prepareCacheFolder(this);
     }
 
+
+    /**
+     * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
+     * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
+     */
     @TargetApi(14)
-    private void setUpRemoteControlClient() {
-        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
+    public static void setUpRemoteControlClient() {
+        Context context = VLCApplication.getAppContext();
+        AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
 
         if(Util.isICSOrLater()) {
             audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
 
-            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
-            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
-            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
+            if (mRemoteControlClient == null) {
+                Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
+                mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
+                PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
+
+                // create and register the remote control client
+                mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
+                audioManager.registerRemoteControlClient(mRemoteControlClient);
+            }
 
-            // create and register the remote control client
-            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
             mRemoteControlClient.setTransportControlFlags(
                     RemoteControlClient.FLAG_KEY_MEDIA_PLAY |
                     RemoteControlClient.FLAG_KEY_MEDIA_PAUSE |
                     RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS |
                     RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
                     RemoteControlClient.FLAG_KEY_MEDIA_STOP);
-            audioManager.registerRemoteControlClient(mRemoteControlClient);
-
         } else if (Util.isFroyoOrLater()) {
             audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
         }
@@ -179,23 +185,10 @@ public class AudioService extends Service {
         if(!Util.isICSOrLater())
             return;
 
-        mRemoteControlClient.setPlaybackState(p);
+        if (mRemoteControlClient != null)
+            mRemoteControlClient.setPlaybackState(p);
     }
 
-   /**
-    * Tell the system we want to be the default receiver for the MEDIA buttons.
-    * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
-    */
-    @TargetApi(8)
-    public static void requestMediaButtons() {
-        if(Util.isFroyoOrLater() && mRemoteControlClientReceiverComponent != null) {
-            Context context = VLCApplication.getAppContext();
-            AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
-            audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
-        }
-    }
-
-
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         updateWidget(this);
@@ -451,6 +444,7 @@ public class AudioService extends Service {
     }
 
     private void pause() {
+        setUpRemoteControlClient();
         mHandler.removeMessages(SHOW_PROGRESS);
         // hideNotification(); <-- see event handler
         mLibVLC.pause();
@@ -458,6 +452,7 @@ public class AudioService extends Service {
 
     private void play() {
         if (mCurrentMedia != null) {
+            setUpRemoteControlClient();
             mLibVLC.play();
             mHandler.sendEmptyMessage(SHOW_PROGRESS);
             showNotification();
@@ -508,6 +503,7 @@ public class AudioService extends Service {
             mLibVLC.readMedia(mCurrentMedia.getLocation(), true);
         }
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
+        setUpRemoteControlClient();
         showNotification();
         updateWidget(this);
         updateRemoteControlClientMetadata();
@@ -518,14 +514,16 @@ public class AudioService extends Service {
         if(!Util.isICSOrLater()) // NOP check
             return;
 
-        MetadataEditor editor = mRemoteControlClient.editMetadata(true);
-        editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mCurrentMedia.getAlbum());
-        editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mCurrentMedia.getArtist());
-        editor.putString(MediaMetadataRetriever.METADATA_KEY_GENRE, mCurrentMedia.getGenre());
-        editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mCurrentMedia.getTitle());
-        editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, mCurrentMedia.getLength());
-        editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, getCover());
-        editor.apply();
+        if (mRemoteControlClient != null) {
+            MetadataEditor editor = mRemoteControlClient.editMetadata(true);
+            editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, mCurrentMedia.getAlbum());
+            editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, mCurrentMedia.getArtist());
+            editor.putString(MediaMetadataRetriever.METADATA_KEY_GENRE, mCurrentMedia.getGenre());
+            editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mCurrentMedia.getTitle());
+            editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, mCurrentMedia.getLength());
+            editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, getCover());
+            editor.apply();
+        }
     }
 
     private void previous() {
@@ -547,6 +545,7 @@ public class AudioService extends Service {
             mLibVLC.readMedia(mCurrentMedia.getLocation(), true);
         }
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
+        setUpRemoteControlClient();
         showNotification();
         updateWidget(this);
         updateRemoteControlClientMetadata();
@@ -709,6 +708,7 @@ public class AudioService extends Service {
                 } else {
                     mLibVLC.readMedia(mCurrentMedia.getLocation());
                 }
+                setUpRemoteControlClient();
                 showNotification();
                 updateWidget(AudioService.this);
                 updateRemoteControlClientMetadata();
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 55ca044..6fffce1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -284,7 +284,7 @@ public class MainActivity extends SherlockFragmentActivity {
         super.onResume();
         mAudioController.addAudioPlayer(mAudioPlayer);
         AudioServiceController.getInstance().bindAudioService(this);
-        AudioService.requestMediaButtons();
+        AudioService.setUpRemoteControlClient();
 
         /* FIXME: this is used to avoid having MainActivity twice in the backstack */
         if (getIntent().hasExtra(AudioService.START_FROM_NOTIFICATION))
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
index ebfc0ea..1dcaf0b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayerActivity.java
@@ -125,7 +125,7 @@ public class AudioPlayerActivity extends Activity implements IAudioPlayer {
         super.onResume();
         AudioServiceController.getInstance().bindAudioService(this);
         mAudioController.addAudioPlayer(this);
-        AudioService.requestMediaButtons();
+        AudioService.setUpRemoteControlClient();
     }
 
     @Override



More information about the Android mailing list