[Android] MediaWrapper: use Media class

Thomas Guillem git at videolan.org
Tue Jan 20 12:05:46 CET 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Thu Jan 15 18:31:48 2015 +0100| [bae6280f08082a86ae622a08182b379a1429fc49] | committer: Thomas Guillem

MediaWrapper: use Media class

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

 vlc-android/src/org/videolan/vlc/MediaWrapper.java |  151 ++++++++++----------
 1 file changed, 72 insertions(+), 79 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/MediaWrapper.java b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
index 029a5c8..d7ec09f 100644
--- a/vlc-android/src/org/videolan/vlc/MediaWrapper.java
+++ b/vlc-android/src/org/videolan/vlc/MediaWrapper.java
@@ -25,7 +25,9 @@ import java.util.Locale;
 import org.videolan.libvlc.util.Extensions;
 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;
@@ -41,7 +43,6 @@ public class MediaWrapper 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;
@@ -59,33 +60,7 @@ public class MediaWrapper 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;
@@ -98,63 +73,50 @@ public class MediaWrapper implements Parcelable {
     private boolean mIsPictureParsed;
     private int mFlags = 0;
 
+
     /**
-     * Create a new Media
+     * Create a new MediaWrapper
      * @param libVLC A pointer to the libVLC instance. Should not be NULL
      * @param URI The URI of the media.
      */
-    public MediaWrapper(LibVLC libVLC, String URI) {
+    public MediaWrapper(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 MediaWrapper
+     * @param media should be parsed
+     */
+    public MediaWrapper(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) {
@@ -166,6 +128,7 @@ public class MediaWrapper implements Parcelable {
                 }
             }
         }
+        updateMeta(media);
     }
 
     private void init(long time, long length, int type,
@@ -221,14 +184,44 @@ public class MediaWrapper 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() {
@@ -443,4 +436,4 @@ public class MediaWrapper implements Parcelable {
             return new MediaWrapper[size];
         }
     };
-}
+}
\ No newline at end of file



More information about the Android mailing list