[Android] [PATCH 09/13] MediaHolder: use Media class

Thomas Guillem thomas at gllm.fr
Thu Jan 15 19:22:26 CET 2015


---
 vlc-android/src/org/videolan/vlc/MediaHolder.java | 155 +++++++++++-----------
 1 file changed, 74 insertions(+), 81 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaHolder.java b/vlc-android/src/org/videolan/vlc/MediaHolder.java
index 4201265..8bb8b75 100644
--- a/vlc-android/src/org/videolan/vlc/MediaHolder.java
+++ b/vlc-android/src/org/videolan/vlc/MediaHolder.java
@@ -25,7 +25,9 @@ import java.util.Locale;
 
 import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcUtil;
-import org.videolan.libvlc.TrackInfo;
+import org.videolan.libvlc.Media;
+import org.videolan.libvlc.Media.VideoTrack;
+import org.videolan.libvlc.Media.Meta;
 
 import android.graphics.Bitmap;
 import android.os.Parcel;
@@ -77,7 +79,6 @@ public class MediaHolder implements Parcelable {
     public final static int TYPE_AUDIO = 1;
     public final static int TYPE_GROUP = 2;
 
-    /** Metadata from libvlc_media */
     protected String mTitle;
     private String mArtist;
     private String mGenre;
@@ -95,33 +96,7 @@ public class MediaHolder implements Parcelable {
     private String mTrackID;
     private String mArtworkURL;
 
-    public final static int libvlc_meta_Title       = 0;
-    public final static int libvlc_meta_Artist      = 1;
-    public final static int libvlc_meta_Genre       = 2;
-//    public final static int libvlc_meta_Copyright   = 3;
-    public final static int libvlc_meta_Album       = 4;
-//    public final static int libvlc_meta_TrackNumber = 5;
-//    public final static int libvlc_meta_Description = 6;
-//    public final static int libvlc_meta_Rating      = 7;
-//    public final static int libvlc_meta_Date        = 8;
-//    public final static int libvlc_meta_Setting     = 9;
-//    public final static int libvlc_meta_URL         = 10;
-//    public final static int libvlc_meta_Language    = 11;
-    public final static int libvlc_meta_NowPlaying  = 12;
-//    public final static int libvlc_meta_Publisher   = 13;
-//    public final static int libvlc_meta_EncodedBy   = 14;
-    public final static int libvlc_meta_ArtworkURL  = 15;
-//    public final static int libvlc_meta_TrackID     = 16;
-//    public final static int libvlc_meta_TrackTotal  = 17;
-//    public final static int libvlc_meta_Director    = 18;
-//    public final static int libvlc_meta_Season      = 19;
-//    public final static int libvlc_meta_Episode     = 20;
-//    public final static int libvlc_meta_ShowName    = 21;
-//    public final static int libvlc_meta_Actors      = 22;
-    public final static int libvlc_meta_AlbumArtist = 23;
-//    public final static int libvlc_meta_DiscNumber  = 24;
-
-    private final String mLocation;
+    private String mLocation;
     private String mFilename;
     private long mTime = 0;
     private int mAudioTrack = -1;
@@ -134,63 +109,50 @@ public class MediaHolder implements Parcelable {
     private boolean mIsPictureParsed;
     private int mFlags = 0;
 
+
     /**
-     * Create a new Media
+     * Create a new MediaHolder
      * @param libVLC A pointer to the libVLC instance. Should not be NULL
      * @param URI The URI of the media.
      */
-    public MediaHolder(LibVLC libVLC, String URI) {
+    public MediaHolder(LibVLC libVLC, String mrl) {
         if(libVLC == null)
             throw new NullPointerException("libVLC was null");
 
-        mLocation = URI;
-
-        mType = TYPE_ALL;
-        TrackInfo[] tracks = libVLC.readTracksInfo(mLocation);
+        final Media media = new Media(libVLC, mrl);
+        media.parse();
+        media.release();
+        init(media);
+    }
 
-        extractTrackInfo(tracks);
+    /**
+     * Create a new MediaHolder
+     * @param media should be parsed
+     */
+    public MediaHolder(Media media) {
+        init(media);
     }
 
-    private void extractTrackInfo(TrackInfo[] tracks) {
-        if (tracks == null) {
-            mTitle = null;
-            mArtist = null;
-            mAlbum = null;
-            mGenre = null;
-            mAlbumArtist = null;
-            return;
-        }
+    private void init(Media media) {
+
+        mLocation = media.getMrl();
+        mLength = media.getDuration();
+
+        mType = TYPE_ALL;
 
-        for (TrackInfo track : tracks) {
-            if (track.Type == TrackInfo.TYPE_VIDEO) {
+        for (int i = 0; i < media.getTrackCount(); ++i) {
+            final Media.Track track = media.getTrack(i);
+            if (track == null)
+                continue;
+            if (track.type == Media.Track.Type.Video) {
+                final Media.VideoTrack videoTrack = (VideoTrack) track;
                 mType = TYPE_VIDEO;
-                mWidth = track.Width;
-                mHeight = track.Height;
-            } else if (mType == TYPE_ALL && track.Type == TrackInfo.TYPE_AUDIO){
+                mWidth = videoTrack.width;
+                mHeight = videoTrack.height;
+            } else if (mType == TYPE_ALL && track.type == Media.Track.Type.Audio){
                 mType = TYPE_AUDIO;
-            } else if (track.Type == TrackInfo.TYPE_META) {
-                mLength = track.Length;
-                mTitle = track.Title != null ? track.Title.trim() : null;
-                mArtist = track.Artist != null ? track.Artist.trim() : null;
-                mAlbum = track.Album != null ? track.Album.trim() : null;
-                mGenre = track.Genre != null ? track.Genre.trim() : null;
-                mAlbumArtist = track.AlbumArtist != null ? track.AlbumArtist.trim() : null;
-                mArtworkURL = track.ArtworkURL;
-                mNowPlaying = track.NowPlaying;
-                if (!TextUtils.isEmpty(track.TrackNumber)) {
-                    try {
-                        mTrackNumber = Integer.parseInt(track.TrackNumber);
-                    } catch (NumberFormatException ignored) {
-                    }
-                }
-                Log.d(TAG, "Title " + mTitle);
-                Log.d(TAG, "Artist " + mArtist);
-                Log.d(TAG, "Genre " + mGenre);
-                Log.d(TAG, "Album " + mAlbum);
             }
         }
-
-        /* No useful ES found */
         if (mType == TYPE_ALL) {
             int dotIndex = mLocation.lastIndexOf(".");
             if (dotIndex != -1) {
@@ -202,6 +164,7 @@ public class MediaHolder implements Parcelable {
                 }
             }
         }
+        updateMeta(media);
     }
 
     private void init(long time, long length, int type,
@@ -257,14 +220,44 @@ public class MediaHolder implements Parcelable {
         return mLocation;
     }
 
+    private static String getMetaId(Media media, int id, boolean trim) {
+        String meta = media.getMeta(id);
+        return meta != null ? trim ? meta.trim() : meta : null;
+    }
+
+    public void updateMeta(Media media) {
+        mTitle = getMetaId(media, Meta.Title, true);
+        mArtist = getMetaId(media, Meta.Artist, true);
+        mAlbum = getMetaId(media, Meta.Album, true);
+        mGenre = getMetaId(media, Meta.Genre, true);
+        mAlbumArtist = getMetaId(media, Meta.AlbumArtist, true);
+        mArtworkURL = getMetaId(media, Meta.ArtworkURL, false);
+        mNowPlaying = getMetaId(media, Meta.NowPlaying, false);
+        final String trackNumber = getMetaId(media, Meta.TrackNumber, false);
+        if (!TextUtils.isEmpty(trackNumber)) {
+            try {
+                mTrackNumber = Integer.parseInt(trackNumber);
+            } catch (NumberFormatException ignored) {
+            }
+        }
+        Log.d(TAG, "Title " + mTitle);
+        Log.d(TAG, "Artist " + mArtist);
+        Log.d(TAG, "Genre " + mGenre);
+        Log.d(TAG, "Album " + mAlbum);
+    }
+
+    /*
+     * XXX to remove
+     */
     public void updateMeta(LibVLC libVLC) {
-        mTitle = libVLC.getMeta(libvlc_meta_Title);
-        mArtist = libVLC.getMeta(libvlc_meta_Artist);
-        mGenre = libVLC.getMeta(libvlc_meta_Genre);
-        mAlbum = libVLC.getMeta(libvlc_meta_Album);
-        mAlbumArtist = libVLC.getMeta(libvlc_meta_AlbumArtist);
-        mNowPlaying = libVLC.getMeta(libvlc_meta_NowPlaying);
-        mArtworkURL = libVLC.getMeta(libvlc_meta_ArtworkURL);
+        mTitle = libVLC.getMeta(Meta.Title);
+        mArtist = libVLC.getMeta(Meta.Artist);
+        mGenre = libVLC.getMeta(Meta.Genre);
+        mAlbum = libVLC.getMeta(Meta.Album);
+        mAlbumArtist = libVLC.getMeta(Meta.AlbumArtist);
+        mNowPlaying = libVLC.getMeta(Meta.NowPlaying);
+        mArtworkURL = libVLC.getMeta(Meta.ArtworkURL);
+
     }
 
     public String getFileName() {
@@ -318,7 +311,7 @@ public class MediaHolder implements Parcelable {
      * Returns the raw picture object. Likely to be NULL in VLC for Android
      * due to lazy-loading.
      *
-     * Use {@link org.videolan.vlc.util.Bitmap#getPictureFromCache(MediaHolder)} instead.
+     * Use {@link org.videolan.vlc.util.Bitmap#getPictureFromCache(Media)} instead.
      *
      * @return The raw picture or NULL
      */
@@ -329,7 +322,7 @@ public class MediaHolder implements Parcelable {
     /**
      * Sets the raw picture object.
      *
-     * In VLC for Android, use {@link org.videolan.vlc.MediaDatabase#setPicture(MediaHolder, Bitmap)} instead.
+     * In VLC for Android, use {@link org.videolan.vlc.MediaDatabase#setPicture(Media, Bitmap)} instead.
      *
      * @param p
      */
@@ -479,4 +472,4 @@ public class MediaHolder implements Parcelable {
             return new MediaHolder[size];
         }
     };
-}
+}
\ No newline at end of file
-- 
2.1.3



More information about the Android mailing list