[vlc-devel] [PATCH] Read file meta data

Rafaël Carré rafael.carre at gmail.com
Mon Aug 15 06:09:16 CEST 2011


For now, only artist, album, title and genre
---
Passing meta as an array indexed by libvlc_meta_t would be better,
but I don't know how to expose the enum to java code, help is welcome

Bugs:
    - artist / album / genre views are empty
    - album field in audio player is empty
    - crashes in FromLocaleDup

Any idea on how to get backtraces for this last one?
I can't find my phone's cable and the simulator is slow...

 extras/package/android/vlc-android/jni/libvlcjni.c |   42 ++++++++++++++++++++
 .../src/org/videolan/vlc/android/LibVLC.java       |    6 +++
 .../src/org/videolan/vlc/android/Media.java        |   22 ++++++++++
 3 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/extras/package/android/vlc-android/jni/libvlcjni.c b/extras/package/android/vlc-android/jni/libvlcjni.c
index cede8f2..82a7fc1 100644
--- a/extras/package/android/vlc-android/jni/libvlcjni.c
+++ b/extras/package/android/vlc-android/jni/libvlcjni.c
@@ -275,6 +275,48 @@ void Java_org_videolan_vlc_android_LibVLC_setEventManager(JNIEnv *env, jobject t
     eventManagerInstance = (*env)->NewGlobalRef(env, eventManager);
 }
 
+jobjectArray Java_org_videolan_vlc_android_LibVLC_readMediaMeta(JNIEnv *env,
+                                    jobject thiz, jint instance, jstring mrl)
+{
+    jboolean isCopy;
+    jobjectArray array = (*env)->NewObjectArray(env, 8,
+            (*env)->FindClass(env, "java/lang/String"),
+            (*env)->NewStringUTF(env, ""));
+
+
+    static const char str[4][7] = {
+        "artist", "album", "title", "genre",
+    };
+    static const libvlc_meta_t meta_id[4] = {
+        libvlc_meta_Artist,
+        libvlc_meta_Album,
+        libvlc_meta_Title,
+        libvlc_meta_Genre,
+    };
+
+    const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy);
+    libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance,
+                                              psz_mrl);
+    libvlc_media_parse(m);
+
+    int i;
+    for (i=0; i < 4 ; i++) {
+        char *meta = libvlc_media_get_meta(m, meta_id[i]);
+        if (!meta)
+            meta = strdup("unknown");
+
+        jstring k = (*env)->NewStringUTF(env, str[i]);
+        (*env)->SetObjectArrayElement(env, array, 2*i, k);
+        jstring v = (*env)->NewStringUTF(env, meta);
+        (*env)->SetObjectArrayElement(env, array, 2*i+1, v);
+
+        free(meta);
+   }
+
+   libvlc_media_release(m);
+   return array;
+}
+
 void Java_org_videolan_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz,
                                        jint instance, jstring mrl)
 {
diff --git a/extras/package/android/vlc-android/src/org/videolan/vlc/android/LibVLC.java b/extras/package/android/vlc-android/src/org/videolan/vlc/android/LibVLC.java
index a4ea3c6..d5c8ab8 100644
--- a/extras/package/android/vlc-android/src/org/videolan/vlc/android/LibVLC.java
+++ b/extras/package/android/vlc-android/src/org/videolan/vlc/android/LibVLC.java
@@ -150,6 +150,11 @@ public class LibVLC {
         readMedia(mLibVlcInstance, mrl);
     }
     
+    public String[] readMediaMeta(String mrl)
+    {
+        return readMediaMeta(mLibVlcInstance, mrl);
+    }
+    
     /**
      * Get a media thumbnail.
      */
@@ -293,6 +298,7 @@ public class LibVLC {
     private native boolean hasVideoTrack(int instance, String filePath);
     
     
+    private native String[] readMediaMeta(int instance, String mrl);
     
     /**
      * Return true if there is a video track in the file
diff --git a/extras/package/android/vlc-android/src/org/videolan/vlc/android/Media.java b/extras/package/android/vlc-android/src/org/videolan/vlc/android/Media.java
index 373143b..4e0b2a9 100644
--- a/extras/package/android/vlc-android/src/org/videolan/vlc/android/Media.java
+++ b/extras/package/android/vlc-android/src/org/videolan/vlc/android/Media.java
@@ -67,6 +67,28 @@ public class Media implements Comparable<Media> {
 			mLibVlc = LibVLC.getInstance();
 			mType = (mLibVlc.hasVideoTrack(file.getPath())) ? TYPE_VIDEO : TYPE_AUDIO;
 			mLength = mLibVlc.getLengthFromFile(file.getPath());
+
+            String[] array = mLibVlc.readMediaMeta(file.getPath());
+
+            int i;
+            for(i=0; i < array.length; i++) {
+                String s = array[i++];
+                String v = array[i];
+
+                if (s.equals("title")) {
+                    mTitle = v;
+                    Log.d(TAG, "Title " + mTitle);
+                } else if (s.equals("artist")) {
+                    mArtist = v;
+                    Log.d(TAG, "Artist " + mArtist);
+                } else if (s.equals("genre")) {
+                    mGenre = v;
+                    Log.d(TAG, "Genre " + mGenre);
+                } else if (s.equals("album")) {
+                    mAlbum = v;
+                    Log.d(TAG, "Album " + mAlbum);
+                }
+            }
 		} catch (LibVlcException e) {
 			e.printStackTrace();
 		}
-- 
1.7.5.4





More information about the vlc-devel mailing list