[Android] [PATCH 03/10] jni: Add expandMedia()

Edward Wang edward.c.wang at compdigitec.com
Wed Aug 29 04:21:51 CEST 2012


This is used to flatten the playlist for things that create subitems, etc, like lua, playlists, etc.
---
 vlc-android/jni/libvlcjni.c                  |   49 ++++++++++++++++++++++++++
 vlc-android/src/org/videolan/vlc/LibVLC.java |   10 +++++
 2 files changed, 59 insertions(+), 0 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 66d5e71..ef2ed0a 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -122,6 +122,8 @@ static void setString(JNIEnv *env, jobject item, const char* field, const char*
     (*env)->SetObjectField(env, item, fieldId, jstr);
 }
 
+static int currentPlaylistIndex;
+
 struct length_change_monitor {
     pthread_mutex_t doneMutex;
     pthread_cond_t doneCondVar;
@@ -324,6 +326,52 @@ end:
         (*myVm)->DetachCurrentThread(myVm);
 }
 
+jint Java_org_videolan_vlc_LibVLC_expandMedia(JNIEnv *env, jobject thiz) {
+    // This is needed to flatten the playlist to allow support for
+    // things that need subitems, like lua demuxers or playlists...
+    // It checks the last playing item and tries to expand it.
+
+    // First, we need to grab LibVLC (Java).
+    jclass clsVLC = (*env)->GetObjectClass(env, thiz);
+
+    jmethodID methodVLC = (*env)->GetStaticMethodID(env, clsVLC, "getExistingInstance", "()Lorg/videolan/vlc/LibVLC;" );
+    if( !methodVLC ) {
+        LOGE("Failed to find method (getExistingInstance)" );
+        return -1;
+    }
+    jobject javaLibVLC = (*env)->CallStaticObjectMethod(env, clsVLC, methodVLC);
+
+    libvlc_media_list_t* p_mlist = getMediaList(env, javaLibVLC);
+    libvlc_media_list_player_t* p_mlp = getMediaListPlayer(env, javaLibVLC);
+
+    libvlc_media_t* p_media = libvlc_media_list_item_at_index( p_mlist, currentPlaylistIndex );
+    if( !p_media )
+        return -1;
+
+    int position = currentPlaylistIndex;
+    libvlc_media_list_t* subitems = libvlc_media_subitems( p_media );
+
+    if(subitems && libvlc_media_list_count( subitems ) > 0) {
+        // Stop the player, since we are going to shuffle the index soon.
+        libvlc_media_list_player_stop( p_mlp );
+
+        libvlc_media_list_remove_index( p_mlist, position );
+        for(int i = 0; i < libvlc_media_list_count( subitems ); i++) {
+            libvlc_media_t* submedia = libvlc_media_list_item_at_index( subitems, i );
+            libvlc_media_list_insert_media( p_mlist, submedia, position+i );
+            libvlc_media_release( submedia );
+        }
+
+        libvlc_media_list_player_play_item_at_index( p_mlp, position );
+        libvlc_media_list_release( subitems );
+        return position;
+    }
+    if(subitems)
+        libvlc_media_list_release( subitems );
+
+    return -1;
+}
+
 jint JNI_OnLoad(JavaVM *vm, void *reserved)
 {
     // Keep a reference on the Java VM.
@@ -613,6 +661,7 @@ static void create_player_and_play(JNIEnv* env, jobject thiz,
     setLong(env, thiz, "mMediaListPlayerInstance", (jlong)p_mlp);
     setLong(env, thiz, "mInternalMediaPlayerInstance", (jlong)mp);
 
+    currentPlaylistIndex = position;
     libvlc_media_list_player_play_item_at_index(p_mlp, position);
 }
 
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index 1e8ec98..ef6d2a1 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -479,6 +479,16 @@ public class LibVLC {
     public native void getMediaListItems(ArrayList<String> arl);
 
     /**
+     * A function to flatten the playlist. This function checks the
+     * currently playing media and sees if it has any submedias under.
+     * If yes, it will return the position of the first flattened media.
+     * Otherwise, it returns -1.
+     *
+     * @return Position of first submedia if yes, otherwise -1
+     */
+    public native int expandMedia();
+
+    /**
      * Return the length of the stream, in milliseconds
      */
     private native long getLengthFromLocation(long instance, String mrl);
-- 
1.7.5.4



More information about the Android mailing list