[Android] [WIP PATCH 10/11] libvlc/util: add MediaBrowser
Thomas Guillem
thomas at gllm.fr
Tue Dec 23 18:38:31 CET 2014
---
.../src/org/videolan/libvlc/util/MediaBrowser.java | 151 +++++++++++++++++++++
1 file changed, 151 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..041e894
--- /dev/null
+++ b/libvlc/src/org/videolan/libvlc/util/MediaBrowser.java
@@ -0,0 +1,151 @@
+package org.videolan.libvlc.util;
+
+import org.videolan.libvlc.LibVLC;
+import org.videolan.libvlc.LibVlcException;
+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 mMediaDiscovererList;
+ private MediaList mMediaList;
+ private MediaList mCurrentMediaList;
+ private Listener mListener;
+
+ public interface Listener {
+ public void onMediaAdded(Media media, int index);
+ public void onMediaRemoved(int index);
+ }
+
+ public MediaBrowser(LibVLC libvlc, Listener listener) {
+ mLibVlc = libvlc; // XXX mLibVlc.retain();
+ mListener = listener;
+ }
+
+ private void reset() {
+ if (mMediaList != null) {
+ mMediaList.release();
+ mMediaList = null;
+ }
+ if (mMediaDiscoverer != null) {
+ mMediaDiscoverer.release();
+ mMediaDiscoverer = null;
+ mMediaDiscovererList = null;
+ }
+ mCurrentMediaList = null;
+ }
+
+ public void release() {
+ reset();
+ }
+
+ public synchronized boolean browse(String mrl) {
+ if (!mrl.contains("://")) {
+ reset();
+ try {
+ if (mrl.equals("smb"))
+ mMediaDiscoverer = new MediaDiscoverer(mLibVlc, "dsm");
+ else
+ mMediaDiscoverer = new MediaDiscoverer(mLibVlc, mrl);
+ mMediaDiscoverer.setListener(mMediaDiscovererListener);
+ mMediaDiscovererList = mMediaDiscoverer.getMediaList();
+ mMediaDiscovererList.setListener(mMediaListListener);
+ mCurrentMediaList = mMediaDiscovererList;
+ mMediaDiscoverer.start();
+ } catch (LibVlcException e) {
+ Log.e(TAG, "browse error: " + e.getMessage());
+ return false;
+ }
+ return true;
+ } else {
+ Media media;
+ try {
+ media = new Media(mLibVlc, mrl);
+ } catch (LibVlcException e) {
+ Log.e(TAG, "browse error: " + e.getMessage());
+ return false;
+ }
+ final boolean ret = browse(media);
+ media.release();
+ return ret;
+ }
+ }
+
+ public synchronized boolean 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();
+ try {
+ mMediaList = new MediaList(mLibVlc);
+ mMediaList.setListener(mMediaListListener);
+ mMediaList.parseStart(media);
+ mCurrentMediaList = mMediaList;
+ } catch (LibVlcException e) {
+ Log.e(TAG, "browse error: " + e.getMessage());
+ return false;
+ } finally {
+ media.release();
+ }
+ return true;
+ }
+
+ public synchronized int getMediaCount() {
+ try {
+ return mCurrentMediaList != null ? mCurrentMediaList.getCount() : 0;
+ } catch (LibVlcException e) {
+ return 0;
+ }
+ }
+
+ public synchronized Media getMediaAt(int index) {
+ try {
+ return mCurrentMediaList != null ? mCurrentMediaList.getMediaAt(index) : null;
+ } catch (LibVlcException e) {
+ return null;
+ }
+ }
+
+ private MediaDiscoverer.Listener mMediaDiscovererListener = new MediaDiscoverer.Listener() {
+
+ @Override
+ public void onEvent(int event, long arg1, long arg2) {
+ switch (event) {
+ case MediaDiscoverer.MediaDiscovererStarted:
+ Log.d(TAG, "MediaDiscovererStarted");
+ break;
+ case MediaDiscoverer.MediaDiscovererEnded:
+ Log.d(TAG, "MediaDiscovererEnded");
+ break;
+ }
+ }
+ };
+
+ private MediaList.Listener mMediaListListener = new MediaList.Listener() {
+
+ @Override
+ public void onEvent(int event, long arg1, long arg2) {
+ if (mListener == null)
+ return;
+ int index = (int) arg1;
+ switch (event) {
+ case MediaList.MediaListItemAdded:
+ try {
+ Media media = mCurrentMediaList.getMediaAt(index);
+ mListener.onMediaAdded(media, index);
+ } catch (LibVlcException e) {}
+ break;
+ case MediaList.MediaListItemDeleted:
+ mListener.onMediaRemoved(index);
+ break;
+ }
+ }
+ };
+}
--
2.1.3
More information about the Android
mailing list