[Android] [PATCH 08/13] libvlc: add MediaBrowser
Thomas Guillem
thomas at gllm.fr
Thu Jan 15 19:22:25 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 | 175 +++++++++++++++++++++
1 file changed, 175 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..7eb37ed
--- /dev/null
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -0,0 +1,175 @@
+/*****************************************************************************
+ * MediaBrowser.java
+ *****************************************************************************
+ * Copyright © 2015 VLC authors and VideoLAN
+ *
+ * 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 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;
+ private boolean mInEvent = false;
+
+ /**
+ * Listener called when medias are added or removed.
+ *
+ * {@link MediaBrowser#browse(Media)}, {@link MediaBrowser#browse(String)},
+ * and {@link MediaBrowser#release()} must NOT be called inside these events methods.
+ */
+ public interface EventListener {
+ public void onMediaAdded(int index, Media media);
+ public void onMediaRemoved(int index);
+ }
+
+ 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.
+ * Must not be called inside {@link EventListener} events.
+ */
+ public synchronized void release() {
+ if (mInEvent)
+ throw new IllegalStateException("release should not be called inside EventListener");
+ reset();
+ }
+
+ /**
+ * Browse to the specified mrl.
+ * Must not be called inside {@link EventListener} events.
+ *
+ * @param mrl
+ */
+ public synchronized void browse(String mrl) {
+ if (mInEvent)
+ throw new IllegalStateException("browse should not be called inside EventListener");
+ if (!mrl.contains("://") && !mrl.startsWith("/")) {
+ reset();
+ if (mrl.equals("smb"))
+ mMediaDiscoverer = new MediaDiscoverer(mLibVlc, "dsm");
+ else
+ mMediaDiscoverer = new MediaDiscoverer(mLibVlc, mrl);
+ mMediaDiscoverer.setEventListener(mMediaDiscovererEventListener);
+ 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.
+ * Must not be called inside {@link EventListener} events.
+ *
+ * @param media Can be a media returned by MediaBrowser.
+ */
+ public synchronized void browse(Media media) {
+ if (mInEvent)
+ throw new IllegalStateException("browse should not be called inside EventListener");
+ /* 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 MediaDiscoverer.EventListener mMediaDiscovererEventListener = new MediaDiscoverer.EventListener() {
+ @Override
+ public void onEvent(int event, long arg1, long arg2) {
+ switch (event) {
+ case MediaDiscoverer.Events.MediaDiscovererStarted:
+ Log.d(TAG, "MediaDiscovererStarted");
+ break;
+ case MediaDiscoverer.Events.MediaDiscovererEnded:
+ Log.d(TAG, "MediaDiscovererEnded");
+ break;
+ }
+ }
+ };
+
+ private MediaList.EventListener mMediaListEventListener = new MediaList.EventListener() {
+ @Override
+ public void onEvent(int event, long arg1, long arg2) {
+ if (mEventListener == null)
+ return;
+ int index = (int) arg1;
+ synchronized (MediaBrowser.this) {
+ mInEvent = true;
+ switch (event) {
+ case MediaList.Events.MediaListItemAdded:
+ final Media media = mMediaList.getMediaAt(index);
+ mEventListener.onMediaAdded(index, media);
+ break;
+ case MediaList.Events.MediaListItemDeleted:
+ mEventListener.onMediaRemoved(index);
+ break;
+ }
+ mInEvent = false;
+ }
+ }
+ };
+}
\ No newline at end of file
--
2.1.3
More information about the Android
mailing list