[Android] [PATCH 09/14] libvlc: add MediaBrowser
Thomas Guillem
thomas at gllm.fr
Fri Jan 16 17:12:09 CET 2015
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.
---
.../src/org/videolan/libvlc/util/MediaBrowser.java | 163 +++++++++++++++++++++
1 file changed, 163 insertions(+)
create mode 100644 libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
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..653f68f
--- /dev/null
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -0,0 +1,163 @@
+/*****************************************************************************
+ * 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;
+
+import android.util.Log;
+
+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.setEventListener(mMediaEventListener);
+ 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 Media.EventListener mMediaEventListener = new Media.EventListener() {
+ @Override
+ public void onEvent(VLCObject.Event event) {
+ if (mEventListener == null)
+ return;
+ switch (event.type) {
+ case Media.Events.MediaParsedChanged:
+ mEventListener.onBrowseEnd();
+ break;
+ }
+ }
+ };
+
+ 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;
+ }
+ }
+ };
+}
--
2.1.3
More information about the Android
mailing list