[Android] libvlc: add MediaBrowser

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:37 2015 +0100| [49d6841a7d4ba88b9ce564b673ba0b04688d12b1] | committer: Thomas Guillem

libvlc: add MediaBrowser

Utility class to browse via a Media or a MRL.

An MRL can be a directory, a path to an archive, a path to a network share, or a
service discovery.

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

 .../src/org/videolan/libvlc/util/MediaBrowser.java |  149 ++++++++++++++++++++
 1 file changed, 149 insertions(+)

diff --git a/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
new file mode 100644
index 0000000..d5b1353
--- /dev/null
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -0,0 +1,149 @@
+/*****************************************************************************
+ * MediaBrowser.java
+ *****************************************************************************
+ * Copyright © 2015 VLC authors, VideoLAN and VideoLabs
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.libvlc.util;
+
+import org.videolan.libvlc.LibVLC;
+import org.videolan.libvlc.Media;
+import org.videolan.libvlc.MediaDiscoverer;
+import org.videolan.libvlc.MediaList;
+import org.videolan.libvlc.VLCObject;
+
+public class MediaBrowser {
+    private static final String TAG = "LibVLC/util/MediaBrowser";
+
+    private LibVLC mLibVlc;
+    private MediaDiscoverer mMediaDiscoverer;
+    private MediaList mMediaList;
+    private Media mMedia;
+    private EventListener mEventListener;
+
+    /**
+     * Listener called when medias are added or removed.
+     */
+    public interface EventListener {
+        public void onMediaAdded(int index, Media media);
+        public void onMediaRemoved(int index);
+        /**
+         * Called when browse ended.
+         * It won't be called when you browse a service discovery.
+         */
+        public void onBrowseEnd();
+    }
+
+    public MediaBrowser(LibVLC libvlc, EventListener listener) {
+        mLibVlc = libvlc; // XXX mLibVlc.retain();
+        mEventListener = listener;
+    }
+
+    private synchronized void reset() {
+        if (mMediaDiscoverer != null) {
+            mMediaDiscoverer.release();
+            mMediaDiscoverer = null;
+        }
+        if (mMedia != null) {
+            mMedia.release();
+            mMedia = null;
+        }
+        /* don't need to release the MediaList since it's either
+         * associated with a Media or a MediaDiscoverer that will release it */
+        mMediaList = null;
+    }
+
+    /**
+     * Release the MediaBrowser.
+     */
+    public synchronized void release() {
+        reset();
+    }
+
+    /**
+     * Browse to the specified mrl.
+     *
+     * @param mrl
+     */
+    public synchronized void browse(String mrl) {
+        if (!mrl.contains("://") && !mrl.startsWith("/")) {
+            reset();
+            if (mrl.equals("smb"))
+                mMediaDiscoverer = new MediaDiscoverer(mLibVlc, "dsm");
+            else
+                mMediaDiscoverer = new MediaDiscoverer(mLibVlc, mrl);
+            mMediaList = mMediaDiscoverer.getMediaList();
+            mMediaList.setEventListener(mMediaListEventListener);
+            mMediaDiscoverer.start();
+        } else {
+            final Media media = new Media(mLibVlc, mrl);
+            browse(media);
+            media.release();
+        }
+    }
+
+    /**
+     * Browse to the specified media.
+     *
+     * @param media Can be a media returned by MediaBrowser.
+     */
+    public synchronized void browse(Media media) {
+        /* media can be associated with a medialist,
+         * so increment ref count in order to don't clean it with the medialist
+         */
+        media.retain();
+        reset();
+        mMediaList = media.subItems();
+        mMediaList.setEventListener(mMediaListEventListener);
+        media.parseAsync(Media.Parse.ParseNetwork);
+        mMedia = media;
+    }
+
+    /**
+     * Get the number or media.
+     */
+    public synchronized int getMediaCount() {
+        return mMediaList != null ? mMediaList.getCount() : 0;
+    }
+
+    /**
+     * Get a media at a specified index.
+     */
+    public synchronized Media getMediaAt(int index) {
+        return mMediaList != null ? mMediaList.getMediaAt(index) : null;
+    }
+
+    private MediaList.EventListener mMediaListEventListener = new MediaList.EventListener() {
+        @Override
+        public void onEvent(VLCObject.Event event) {
+            if (mEventListener == null)
+                return;
+            final MediaList.Event mlEvent = (MediaList.Event) event;
+
+            switch (mlEvent.type) {
+            case MediaList.Events.MediaListItemAdded:
+                mEventListener.onMediaAdded(mlEvent.index, mlEvent.media);
+                break;
+            case MediaList.Events.MediaListItemDeleted:
+                mEventListener.onMediaRemoved(mlEvent.index);
+                break;
+            case MediaList.Events.MediaListEndReached:
+                mEventListener.onBrowseEnd();
+            }
+        }
+    };
+}



More information about the Android mailing list