[Android] [PATCH 09/24] jni: Factorize readMedia and add more media list functions

Edward Wang edward.c.wang at compdigitec.com
Wed Aug 22 23:15:09 CEST 2012


---
 vlc-android/jni/libvlcjni.c                  |   68 ++++++++++++++++++-------
 vlc-android/src/org/videolan/vlc/LibVLC.java |   19 +++++++
 2 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/vlc-android/jni/libvlcjni.c b/vlc-android/jni/libvlcjni.c
index 7ea4972..0c66720 100644
--- a/vlc-android/jni/libvlcjni.c
+++ b/vlc-android/jni/libvlcjni.c
@@ -558,20 +558,11 @@ jobjectArray Java_org_videolan_vlc_LibVLC_readMediaMeta(JNIEnv *env,
    return array;
 }
 
-void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
-                                            jlong instance, jstring mrl, jboolean novideo)
-{
+static void create_player_and_play(JNIEnv* env, jobject thiz,
+                                   jlong instance, int position) {
     /* Release previous media player, if any */
     releaseMediaPlayer(env, thiz);
 
-    /* Create a new item */
-    libvlc_media_t *m = new_media(instance, env, thiz, mrl, false, novideo);
-    if (!m)
-    {
-        LOGE("readMedia: Could not create the media!");
-        return;
-    }
-
     libvlc_media_list_t* p_mlist = getMediaList(env, thiz);
 
     /* Create a media player playing environment */
@@ -580,11 +571,6 @@ void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
 
     jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);
 
-    libvlc_media_list_lock(p_mlist);
-    libvlc_media_list_add_media(p_mlist, m);
-    int position = libvlc_media_list_index_of_item(p_mlist, m);
-    libvlc_media_list_unlock(p_mlist);
-
     //if AOUT_AUDIOTRACK_JAVA, we use amem
     jclass cls = (*env)->GetObjectClass(env, thiz);
     jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
@@ -595,9 +581,6 @@ void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
         libvlc_audio_set_format_callbacks(mp, aout_open, aout_close);
     }
 
-    /* No need to keep the media now */
-    libvlc_media_release(m);
-
     /* Connect the event manager */
     libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);
     static const libvlc_event_type_t mp_events[] = {
@@ -620,6 +603,53 @@ void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
     libvlc_media_list_player_play_item_at_index(p_mlp, position);
 }
 
+void Java_org_videolan_vlc_LibVLC_readMedia(JNIEnv *env, jobject thiz,
+                                            jlong instance, jstring mrl, jboolean novideo)
+{
+    /* Create a new item */
+    libvlc_media_t *m = new_media(instance, env, thiz, mrl, false, novideo);
+    if (!m)
+    {
+        LOGE("readMedia: Could not create the media!");
+        return;
+    }
+
+    libvlc_media_list_t* p_mlist = getMediaList(env, thiz);
+
+    libvlc_media_list_lock(p_mlist);
+    libvlc_media_list_add_media(p_mlist, m);
+    int position = libvlc_media_list_index_of_item(p_mlist, m);
+    libvlc_media_list_unlock(p_mlist);
+
+    /* No need to keep the media now */
+    libvlc_media_release(m);
+
+    create_player_and_play(env, thiz, instance, position);
+}
+
+void Java_org_videolan_vlc_LibVLC_playIndex(JNIEnv *env, jobject thiz,
+                                            jlong instance, int position) {
+    create_player_and_play(env, thiz, instance, position);
+}
+
+void Java_org_videolan_vlc_LibVLC_getMediaListItems(
+                JNIEnv *env, jobject thiz, jobject arrayList) {
+    jclass arrayClass = (*env)->FindClass(env, "java/util/ArrayList");
+    jmethodID methodID = (*env)->GetMethodID(env, arrayClass, "add", "(Ljava/lang/Object;)Z");
+    jstring str;
+
+    libvlc_media_list_t* p_mlist = getMediaList(env, thiz);
+    libvlc_media_list_lock( p_mlist );
+    for(int i = 0; i < libvlc_media_list_count( p_mlist ); i++) {
+        char* mrl = libvlc_media_get_mrl( libvlc_media_list_item_at_index( p_mlist, i ) );
+        str = (*env)->NewStringUTF(env, mrl);
+        (*env)->CallBooleanMethod(env, arrayList, methodID, str);
+        (*env)->DeleteLocalRef(env, str);
+        free(mrl);
+    }
+    libvlc_media_list_unlock( p_mlist );
+}
+
 jfloat Java_org_videolan_vlc_LibVLC_getRate(JNIEnv *env, jobject thiz) {
     libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
     if(mp)
diff --git a/vlc-android/src/org/videolan/vlc/LibVLC.java b/vlc-android/src/org/videolan/vlc/LibVLC.java
index 95e7404..45f722a 100644
--- a/vlc-android/src/org/videolan/vlc/LibVLC.java
+++ b/vlc-android/src/org/videolan/vlc/LibVLC.java
@@ -252,6 +252,15 @@ public class LibVLC {
         readMedia(mLibVlcInstance, mrl, novideo);
     }
 
+    /**
+     * Play a media from the media list (playlist)
+     *
+     * @param position The index of the media
+     */
+    public void playIndex(int position) {
+        playIndex(mLibVlcInstance, position);
+    }
+
     public String[] readMediaMeta(String mrl) {
         return readMediaMeta(mLibVlcInstance, mrl);
     }
@@ -319,6 +328,11 @@ public class LibVLC {
     private native void readMedia(long instance, String mrl, boolean novideo);
 
     /**
+     * Play an index in the native media list (playlist)
+     */
+    private native void playIndex(long instance, int position);
+
+    /**
      * Return true if there is currently a running media player.
      */
     public native boolean hasMediaPlayer();
@@ -458,6 +472,11 @@ public class LibVLC {
     public native boolean nativeIsPathDirectory(String path);
 
     /**
+     * Get the list of existing items in the media list (playlist)
+     */
+    public native void getMediaListItems(ArrayList<String> arl);
+
+    /**
      * 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