[Android] VideoPlayerActivity: switch to audio mode when an audio file is opened from an external application.

Adrien Maglo git at videolan.org
Thu Feb 20 15:25:00 CET 2014


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Thu Feb 20 15:24:36 2014 +0100| [925e8b8347376f1e9cc56c52eb96cb99074f45f5] | committer: Adrien Maglo

VideoPlayerActivity: switch to audio mode when an audio file is opened from an external application.

close #10020

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

 vlc-android/jni/libvlcjni.c                        |    9 ++++++++-
 .../src/org/videolan/libvlc/EventHandler.java      |    2 +-
 vlc-android/src/org/videolan/vlc/AudioService.java |    3 +++
 .../vlc/gui/video/VideoPlayerActivity.java         |   21 ++++++++++++++++++++
 4 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 1f4a5f6..db69733 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -370,7 +370,6 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong in
     for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
         libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);
 
-
     /* Keep a pointer to this media player */
     setLong(env, thiz, "mInternalMediaPlayerInstance", (jlong)(intptr_t)mp);
 
@@ -396,6 +395,14 @@ void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong in
 
     (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
 
+    /* Connect the media event manager. */
+    libvlc_event_manager_t *ev_media = libvlc_media_event_manager(p_md);
+    static const libvlc_event_type_t mp_media_events[] = {
+        libvlc_MediaParsedChanged
+    };
+    for(int i = 0; i < (sizeof(mp_media_events) / sizeof(*mp_media_events)); i++)
+        libvlc_event_attach(ev_media, mp_media_events[i], vlc_event_callback, myVm);
+
     libvlc_media_player_set_media(mp, p_md);
     libvlc_media_player_play(mp);
 }
diff --git a/vlc-android/src/org/videolan/libvlc/EventHandler.java b/vlc-android/src/org/videolan/libvlc/EventHandler.java
index 0ebd30d..5c365a6 100644
--- a/vlc-android/src/org/videolan/libvlc/EventHandler.java
+++ b/vlc-android/src/org/videolan/libvlc/EventHandler.java
@@ -35,7 +35,7 @@ public class EventHandler {
     //public static final int MediaMetaChanged                = 0;
     //public static final int MediaSubItemAdded               = 1;
     //public static final int MediaDurationChanged            = 2;
-    //public static final int MediaParsedChanged              = 3;
+    public static final int MediaParsedChanged                = 3;
     //public static final int MediaFreed                      = 4;
     //public static final int MediaStateChanged               = 5;
 
diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index 0508718..413df73 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -401,6 +401,9 @@ public class AudioService extends Service {
             if(service == null) return;
 
             switch (msg.getData().getInt("event")) {
+                case EventHandler.MediaParsedChanged:
+                    Log.i(TAG, "MediaParsedChanged");
+                    break;
                 case EventHandler.MediaPlayerPlaying:
                     Log.i(TAG, "MediaPlayerPlaying");
                     service.executeUpdate();
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 0b33afe..bc5a7ce 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -51,6 +51,7 @@ import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.WeakHandler;
 import org.videolan.vlc.gui.CommonDialogs;
 import org.videolan.vlc.gui.CommonDialogs.MenuType;
+import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.PreferencesActivity;
 
 import android.annotation.TargetApi;
@@ -747,8 +748,17 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         public void handleMessage(Message msg) {
             VideoPlayerActivity activity = getOwner();
             if(activity == null) return;
+            // Do not handle events if we are leaving the VideoPlayerActivity
+            if (activity.mSwitchingView) return;
 
             switch (msg.getData().getInt("event")) {
+                case EventHandler.MediaParsedChanged:
+                    Log.i(TAG, "MediaParsedChanged");
+                    if (activity.mLibVLC.getVideoTracksCount() < 1) {
+                        Log.i(TAG, "No video track, open in audio mode");
+                        activity.switchToAudioMode();
+                    }
+                    break;
                 case EventHandler.MediaPlayerPlaying:
                     Log.i(TAG, "MediaPlayerPlaying");
                     activity.showOverlay();
@@ -904,6 +914,17 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         }
     }
 
+    private void switchToAudioMode() {
+        mSwitchingView = true;
+        // Show the MainActivity if it is not in background.
+        if (getIntent().getAction() != null
+            && getIntent().getAction().equals(Intent.ACTION_VIEW)) {
+            Intent i = new Intent(this, MainActivity.class);
+            startActivity(i);
+        }
+        finish();
+    }
+
     private void changeSurfaceSize() {
         int sw;
         int sh;



More information about the Android mailing list