[Android] Option: background playback for videos

Geoffrey Métais git at videolan.org
Mon Apr 13 16:50:12 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Apr 13 16:49:22 2015 +0200| [91005a6a3dc6853ccc22f9b1ea50391b3db199b0] | committer: Geoffrey Métais

Option: background playback for videos

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

 vlc-android/res/values/strings.xml                          |    2 ++
 vlc-android/res/xml/preferences.xml                         |    6 ++++++
 vlc-android/src/org/videolan/vlc/audio/AudioService.java    |    5 +++++
 .../src/org/videolan/vlc/audio/AudioServiceController.java  |    5 +++++
 .../src/org/videolan/vlc/gui/PreferencesActivity.java       |    2 ++
 vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java |    8 ++++++++
 .../src/org/videolan/vlc/gui/video/AdvOptionsDialog.java    |    2 +-
 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.java |   11 ++++++++---
 .../org/videolan/vlc/interfaces/IAudioPlayerControl.java    |    2 ++
 .../src/org/videolan/vlc/interfaces/IAudioService.aidl      |    1 +
 10 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 2888f36..70749a2 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -26,6 +26,8 @@
     <string name="play_from_start">Play from start</string>
     <string name="play_as_audio">Play as audio</string>
     <string name="play_as_video">Play as video</string>
+    <string name="play_as_audio_background">Play videos in background</string>
+    <string name="play_as_audio_background_summary">Continue video playback in background when turning device screen off or clicking HOME button.</string>
     <string name="append">Append</string>
     <string name="play_all">Play all</string>
     <string name="append_all">Append all</string>
diff --git a/vlc-android/res/xml/preferences.xml b/vlc-android/res/xml/preferences.xml
index 1136f6f..59ead1b 100644
--- a/vlc-android/res/xml/preferences.xml
+++ b/vlc-android/res/xml/preferences.xml
@@ -53,6 +53,12 @@
             <PreferenceCategory android:title="@string/interface_other_category" >
                 <CheckBoxPreference
                     android:defaultValue="false"
+                    android:key="video_background"
+                    android:summary="@string/play_as_audio_background_summary"
+                    android:widgetLayout="@layout/custom_chexbox"
+                    android:title="@string/play_as_audio_background" />
+                <CheckBoxPreference
+                    android:defaultValue="false"
                     android:key="dialog_confirm_resume"
                     android:summary="@string/confirm_resume_summary"
                     android:widgetLayout="@layout/custom_chexbox"
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index e70d325..490924c 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -1449,6 +1449,11 @@ public class AudioService extends Service {
         public float getRate() throws RemoteException {
             return mLibVLC.getRate();
         }
+
+        @Override
+        public void handleVout() throws RemoteException {
+            AudioService.this.handleVout();
+        }
     };
 
     private void updateWidget(Context context) {
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
index 6df5fba..bf70219 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
@@ -508,4 +508,9 @@ public class AudioServiceController implements IAudioPlayerControl {
     public float getRate() {
         return remoteProcedureCall(mAudioServiceBinder, Float.class, (float) 1.0, "getRate", null, null);
     }
+
+    @Override
+    public void handleVout() {
+        remoteProcedureCall(mAudioServiceBinder, Void.class, (Void)null, "handleVout", null, null);
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
index 89e5e37..d7e4f83 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
@@ -74,6 +74,8 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
     public final static String VIDEO_SUBTITLE_FILES = "VideoSubtitleFiles";
     public final static String VIDEO_LAST = "VideoLastPlayed";
     public final static String VIDEO_SPEED = "VideoSpeed";
+    public final static String VIDEO_BACKGROUND = "video_background";
+    public final static String VIDEO_RESTORE = "video_restore";
     public final static int RESULT_RESCAN = RESULT_FIRST_USER + 1;
     public final static int RESULT_RESTART = RESULT_FIRST_USER + 2;
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
index f1597c2..136eaf6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
@@ -33,6 +33,7 @@ import org.videolan.vlc.audio.RepeatType;
 import org.videolan.vlc.gui.CommonDialogs;
 import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.CommonDialogs.MenuType;
+import org.videolan.vlc.gui.PreferencesActivity;
 import org.videolan.vlc.gui.audio.widget.CoverMediaSwitcher;
 import org.videolan.vlc.gui.audio.widget.HeaderMediaSwitcher;
 import org.videolan.vlc.gui.dialogs.SavePlaylist;
@@ -42,6 +43,7 @@ import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.Util;
 import org.videolan.vlc.widget.AudioMediaSwitcher.AudioMediaSwitcherListener;
 
+import android.content.SharedPreferences;
 import android.media.AudioManager;
 import android.os.Bundle;
 import android.os.Handler;
@@ -315,6 +317,12 @@ public class AudioPlayer extends Fragment implements IAudioPlayer, View.OnClickL
             return;
 
         if (mAudioController.hasMedia()) {
+            SharedPreferences mSettings= PreferenceManager.getDefaultSharedPreferences(getActivity());
+            if (mSettings.getBoolean(PreferencesActivity.VIDEO_RESTORE, false)){
+                Util.commitPreferences(mSettings.edit().putBoolean(PreferencesActivity.VIDEO_RESTORE, false));
+                mAudioController.handleVout();
+                return;
+            }
             show();
         } else {
             hide();
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/AdvOptionsDialog.java b/vlc-android/src/org/videolan/vlc/gui/video/AdvOptionsDialog.java
index 52570b3..7626660 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/AdvOptionsDialog.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/AdvOptionsDialog.java
@@ -396,7 +396,7 @@ public class AdvOptionsDialog extends DialogFragment implements View.OnClickList
                 mHandler.sendEmptyMessage(TOGGLE_CANCEL);
                 break;
             case R.id.playback_switch_audio:
-                ((VideoPlayerActivity)getActivity()).switchToAudioMode();
+                ((VideoPlayerActivity)getActivity()).switchToAudioMode(true);
                 break;
             case R.id.opt_equalizer:
                 ((MainActivity)getActivity()).showSecondaryFragment("equalizer");
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 6b0a222..c097926 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -564,6 +564,10 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
 
         if (mAlertDialog != null && mAlertDialog.isShowing())
             mAlertDialog.dismiss();
+        if (!isFinishing() && mSettings.getBoolean(PreferencesActivity.VIDEO_BACKGROUND, false)) {
+            Util.commitPreferences(mSettings.edit().putBoolean(PreferencesActivity.VIDEO_RESTORE, true));
+            switchToAudioMode(false);
+        }
         stopPlayback();
 
         // Dismiss the presentation when the activity is not visible.
@@ -1460,7 +1464,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 case EventHandler.MediaPlayerESAdded:
                     if (!activity.mHasMenu && activity.mLibVLC.getVideoTracksCount() < 1) {
                         Log.i(TAG, "No video track, open in audio mode");
-                        activity.switchToAudioMode();
+                        activity.switchToAudioMode(true);
                     }
                     // no break here, we want to invalidate tracks
                 case EventHandler.MediaPlayerESDeleted:
@@ -1613,12 +1617,13 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         }
     }
 
-    public void switchToAudioMode() {
+    public void switchToAudioMode(boolean showUI) {
         if (mHardwareAccelerationError)
             return;
         mSwitchingView = true;
+        mLibVLC.setVideoTrack(-1);
         // Show the MainActivity if it is not in background.
-        if (getIntent().getAction() != null
+        if (showUI && getIntent().getAction() != null
             && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
             Intent i = new Intent(this, MainActivity.class);
             if (!Util.isCallable(i)){
diff --git a/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayerControl.java b/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayerControl.java
index 64cc1b4..41f36b2 100644
--- a/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayerControl.java
+++ b/vlc-android/src/org/videolan/vlc/interfaces/IAudioPlayerControl.java
@@ -76,4 +76,6 @@ public interface IAudioPlayerControl {
     void detectHeadset(boolean enable);
 
     float getRate();
+
+    void handleVout();
 }
diff --git a/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl b/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
index a2b5fc0..d091362 100644
--- a/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
+++ b/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
@@ -63,4 +63,5 @@ interface IAudioService {
     void showWithoutParse(int index);
     void playIndex(int index);
     float getRate();
+    void handleVout();
 }



More information about the Android mailing list