[Android] Fix remote control for Android 2
Geoffrey Métais
git at videolan.org
Thu Mar 30 14:26:00 CEST 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Mar 30 14:25:34 2017 +0200| [2494b8dd8cc0051c978ff7afe53b2de022ff4d4e] | committer: Geoffrey Métais
Fix remote control for Android 2
> https://code.videolan.org/videolan/vlc-android/commit/2494b8dd8cc0051c978ff7afe53b2de022ff4d4e
---
.../src/org/videolan/vlc/PlaybackService.java | 26 +++++++++++++++++-----
.../videolan/vlc/RemoteControlClientReceiver.java | 23 ++++++++++---------
2 files changed, 33 insertions(+), 16 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/PlaybackService.java b/vlc-android/src/org/videolan/vlc/PlaybackService.java
index d588dfa..3e8ed5e 100644
--- a/vlc-android/src/org/videolan/vlc/PlaybackService.java
+++ b/vlc-android/src/org/videolan/vlc/PlaybackService.java
@@ -1,7 +1,7 @@
/*****************************************************************************
* PlaybackService.java
*****************************************************************************
- * Copyright © 2011-2015 VLC authors and VideoLAN
+ * Copyright © 2011-2017 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -294,9 +294,13 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
if (intent == null)
return START_STICKY;
String action = intent.getAction();
- if (ACTION_REMOTE_PLAYPAUSE.equals(action)){
+ if (Intent.ACTION_MEDIA_BUTTON.equals(action)) {
+ MediaButtonReceiver.handleIntent(mMediaSession, intent);
+ return START_STICKY;
+ }
+ if (ACTION_REMOTE_PLAYPAUSE.equals(action)) {
if (hasCurrentMedia())
- return START_STICKY;
+ return super.onStartCommand(intent, flags, startId);
else
loadLastPlaylist(TYPE_AUDIO);
} else if (ACTION_REMOTE_PLAY.equals(action)) {
@@ -439,7 +443,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
*/
if (action.equalsIgnoreCase(ACTION_REMOTE_PLAYPAUSE)) {
if (!hasCurrentMedia())
- return;
+ loadLastPlaylist(TYPE_AUDIO);
if (mMediaPlayer.isPlaying())
pause();
else
@@ -1066,7 +1070,7 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
private void initMediaSession() {
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
- mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
+ mediaButtonIntent.setClass(this, RemoteControlClientReceiver.class);
PendingIntent mbrIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
mSessionCallback = new MediaSessionCallback();
@@ -1098,7 +1102,8 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event != null) {
int keyCode = event.getKeyCode();
- if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE || keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
+ if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE
+ || keyCode == KeyEvent.KEYCODE_HEADSETHOOK || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
long time = SystemClock.uptimeMillis();
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
@@ -1127,6 +1132,15 @@ public class PlaybackService extends MediaBrowserServiceCompat implements IVLCVo
break;
}
return false;
+ } else if (!AndroidUtil.isHoneycombOrLater) {
+ switch (keyCode) {
+ case KeyEvent.KEYCODE_MEDIA_NEXT:
+ onSkipToNext();
+ return true;
+ case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
+ onSkipToPrevious();
+ return true;
+ }
}
}
return false;
diff --git a/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java b/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
index 2fbc417..b1406ab 100644
--- a/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
+++ b/vlc-android/src/org/videolan/vlc/RemoteControlClientReceiver.java
@@ -1,7 +1,7 @@
/*****************************************************************************
* RemoteControlClientReceiver.java
* ****************************************************************************
- * Copyright © 2012 VLC authors and VideoLAN
+ * Copyright © 2012-2017 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -19,10 +19,10 @@
*****************************************************************************/
package org.videolan.vlc;
-import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
+import android.support.v4.media.session.MediaButtonReceiver;
import android.view.KeyEvent;
import org.videolan.vlc.util.AndroidDevices;
@@ -30,7 +30,7 @@ import org.videolan.vlc.util.AndroidDevices;
/**
* Small class to receive events passed out by the remote controls (wired, bluetooth, lock screen, ...)
*/
-public class RemoteControlClientReceiver extends BroadcastReceiver {
+public class RemoteControlClientReceiver extends MediaButtonReceiver {
@SuppressWarnings("unused")
private static final String TAG = "VLC/RemoteControlClientReceiver";
@@ -42,16 +42,15 @@ public class RemoteControlClientReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
- if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
-
- KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
- if (event == null)
- return;
+ KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
+ if (event != null && action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
if (event.getKeyCode() != KeyEvent.KEYCODE_HEADSETHOOK &&
event.getKeyCode() != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE &&
- event.getAction() != KeyEvent.ACTION_DOWN)
+ event.getAction() != KeyEvent.ACTION_DOWN) {
+ super.onReceive(context, intent);
return;
+ }
Intent i = null;
switch (event.getKeyCode()) {
@@ -105,12 +104,16 @@ public class RemoteControlClientReceiver extends BroadcastReceiver {
if (isOrderedBroadcast())
abortBroadcast();
- if (i != null)
+ if (i != null) {
context.startService(i);
+ return;
+ }
} else if (action.equals(PlaybackService.ACTION_REMOTE_PLAYPAUSE)) {
intent = new Intent(context, PlaybackService.class);
intent.setAction(PlaybackService.ACTION_REMOTE_PLAYPAUSE);
context.startService(intent);
+ return;
}
+ super.onReceive(context, intent);
}
}
More information about the Android
mailing list