[Android] [PATCH 3/3] libvlc: remove MediaList singletons

Thomas Guillem thomas at gllm.fr
Mon Jan 5 15:28:18 CET 2015


Add MediaListPlayer: a wrapper that handle MediaList and play items of the
list.
---
 libvlc/src/org/videolan/libvlc/EventHandler.java   |   2 -
 libvlc/src/org/videolan/libvlc/LibVLC.java         |  54 +--------
 libvlc/src/org/videolan/libvlc/MediaList.java      |  22 ----
 .../src/org/videolan/libvlc/MediaListPlayer.java   |  89 +++++++++++++++
 .../src/org/videolan/vlc/audio/AudioService.java   | 124 ++++++++++++---------
 .../videolan/vlc/audio/AudioServiceController.java |   5 +
 .../org/videolan/vlc/gui/audio/AudioPlayer.java    |  27 ++---
 .../vlc/gui/video/VideoPlayerActivity.java         |  15 ++-
 .../org/videolan/vlc/interfaces/IAudioService.aidl |   2 +
 9 files changed, 190 insertions(+), 150 deletions(-)
 create mode 100644 libvlc/src/org/videolan/libvlc/MediaListPlayer.java

diff --git a/libvlc/src/org/videolan/libvlc/EventHandler.java b/libvlc/src/org/videolan/libvlc/EventHandler.java
index 4ec0861..6242b2d 100644
--- a/libvlc/src/org/videolan/libvlc/EventHandler.java
+++ b/libvlc/src/org/videolan/libvlc/EventHandler.java
@@ -88,8 +88,6 @@ public class EventHandler {
     //public static final int VlmMediaInstanceStatusEnd       = 0x609;
     //public static final int VlmMediaInstanceStatusError     = 0x60a;
 
-    public static final int CustomMediaListExpanding          = 0x2000;
-    public static final int CustomMediaListExpandingEnd       = 0x2001;
     public static final int CustomMediaListItemAdded          = 0x2002;
     public static final int CustomMediaListItemDeleted        = 0x2003;
     public static final int CustomMediaListItemMoved          = 0x2004;
diff --git a/libvlc/src/org/videolan/libvlc/LibVLC.java b/libvlc/src/org/videolan/libvlc/LibVLC.java
index d60c479..8632b4a 100644
--- a/libvlc/src/org/videolan/libvlc/LibVLC.java
+++ b/libvlc/src/org/videolan/libvlc/LibVLC.java
@@ -63,12 +63,9 @@ public class LibVLC {
 
     /** libVLC instance C pointer */
     private long mLibVlcInstance = 0; // Read-only, reserved for JNI
-    /** libvlc_media_player pointer and index */
-    private int mInternalMediaPlayerIndex = 0; // Read-only, reserved for JNI
+    /** libvlc_media_player pointer */
     private long mInternalMediaPlayerInstance = 0; // Read-only, reserved for JNI
 
-    private MediaList mMediaList; // Pointer to media list being followed
-
     /** Buffer for VLC messages */
     private StringBuffer mDebugLogBuffer;
     private boolean mIsBufferingLog = false;
@@ -212,15 +209,6 @@ public class LibVLC {
     }
 
     /**
-     * Get the media list that LibVLC is following right now.
-     *
-     * @return The media list object being followed
-     */
-    public MediaList getMediaList() {
-        return mMediaList;
-    }
-
-    /**
      * Give to LibVLC the surface to draw the video.
      * @param f the surface to draw
      */
@@ -515,7 +503,6 @@ public class LibVLC {
             File cacheDir = context.getCacheDir();
             mCachePath = (cacheDir != null) ? cacheDir.getAbsolutePath() : null;
             nativeInit();
-            mMediaList = new MediaList(this);
             setEventHandler(EventHandler.getInstance());
             mIsInitialized = true;
         }
@@ -568,22 +555,6 @@ public class LibVLC {
     }
 
     /**
-     * Play a media from the media list (playlist)
-     *
-     * @param position The index of the media
-     */
-    public void playIndex(int position) {
-        String mrl = mMediaList.getMRL(position);
-        if (mrl == null)
-            return;
-
-        final Media media = mMediaList.getMedia(position);
-        String[] options = getMediaOptions(media);
-        mInternalMediaPlayerIndex = position;
-        playMRL(mrl, options);
-    }
-
-    /**
      * Play an MRL directly.
      *
      * @param mrl MRL of the media to play.
@@ -591,7 +562,6 @@ public class LibVLC {
     public void playMRL(String mrl) {
         // index=-1 will return options from libvlc instance without relying on MediaList
         String[] options = getMediaOptions(false, false);
-        mInternalMediaPlayerIndex = 0;
         playMRL(mrl, options);
     }
 
@@ -639,7 +609,7 @@ public class LibVLC {
     /**
      * Play an mrl
      */
-    private native void playMRL(String mrl, String[] mediaOptions);
+    public native void playMRL(String mrl, String[] mediaOptions);
 
     /**
      * Returns true if any media is playing
@@ -790,26 +760,6 @@ public class LibVLC {
 
     public native static boolean nativeIsPathDirectory(String path);
 
-     /**
-      * Expand and continue playing the current media.
-      *
-      * @return the index of the media was expanded, and -1 if no media was expanded
-      */
-    public int expandAndPlay() {
-        int r = mMediaList.expandMedia(mInternalMediaPlayerIndex);
-        if(r == 0)
-            this.playIndex(mInternalMediaPlayerIndex);
-        return r;
-    }
-
-    /**
-     * Expand the current media.
-     * @return the index of the media was expanded, and -1 if no media was expanded
-     */
-    public int expand() {
-        return mMediaList.expandMedia(mInternalMediaPlayerIndex);
-    }
-
     private native void setEventHandler(EventHandler eventHandler);
 
     private native void detachEventHandler();
diff --git a/libvlc/src/org/videolan/libvlc/MediaList.java b/libvlc/src/org/videolan/libvlc/MediaList.java
index 408a609..8419c5c 100644
--- a/libvlc/src/org/videolan/libvlc/MediaList.java
+++ b/libvlc/src/org/videolan/libvlc/MediaList.java
@@ -61,28 +61,6 @@ public class MediaList {
         return position >= 0 && position < mInternalList.size();
     }
 
-    /**
-     * This function checks the currently playing media for subitems at the given
-     * position, and if any exist, it will expand them at the same position
-     * and replace the current media.
-     *
-     * @param position The position to expand
-     * @return -1 if no subitems were found, 0 if subitems were expanded
-     */
-    public int expandMedia(int position) {
-        ArrayList<String> children = new ArrayList<String>();
-        int ret = mLibVLC.expandMedia(children);
-        if(ret == 0) {
-            mEventHandler.callback(EventHandler.CustomMediaListExpanding, new Bundle());
-            this.remove(position);
-            for(String mrl : children) {
-                this.insert(position, mrl);
-            }
-            mEventHandler.callback(EventHandler.CustomMediaListExpandingEnd, new Bundle());
-        }
-        return ret;
-    }
-
     public void insert(int position, String mrl) {
         insert(position, new Media(mLibVLC, mrl));
     }
diff --git a/libvlc/src/org/videolan/libvlc/MediaListPlayer.java b/libvlc/src/org/videolan/libvlc/MediaListPlayer.java
new file mode 100644
index 0000000..56ec5a7
--- /dev/null
+++ b/libvlc/src/org/videolan/libvlc/MediaListPlayer.java
@@ -0,0 +1,89 @@
+/*****************************************************************************
+ * MediaListPlayer.java
+ *****************************************************************************
+ * Copyright © 2011-2014 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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;
+
+import java.util.ArrayList;
+
+
+public class MediaListPlayer {
+
+    private int mPlayerIndex = 0;
+    final private LibVLC mLibVLC;
+    final private MediaList mMediaList;
+
+    public MediaListPlayer(LibVLC libVLC) {
+        mLibVLC = libVLC;
+        mMediaList = new MediaList(libVLC);
+    }
+
+    public MediaList getMediaList() {
+        return mMediaList;
+    }
+
+    /**
+     * Play a media from the media list (playlist)
+     *
+     * @param position The index of the media
+     */
+    public void playIndex(int position) {
+        String mrl = mMediaList.getMRL(position);
+        if (mrl == null)
+            return;
+
+        final Media media = mMediaList.getMedia(position);
+        String[] options = mLibVLC.getMediaOptions(media);
+        mPlayerIndex = position;
+        mLibVLC.playMRL(mrl, options);
+    }
+
+    /**
+     * Expand and continue playing the current media.
+     *
+     * @return the index of the media was expanded, and -1 if no media was expanded
+     */
+   public int expandAndPlay() {
+       int r = expand();
+       if(r == 0)
+           playIndex(mPlayerIndex);
+       return r;
+   }
+
+   /**
+    * Expand the current media.
+    * @return the index of the media was expanded, and -1 if no media was expanded
+    */
+   public int expand() {
+       ArrayList<String> children = new ArrayList<String>();
+       int ret = mLibVLC.expandMedia(children);
+       if(ret == 0) {
+           mMediaList.remove(mPlayerIndex);
+           for(String mrl : children) {
+               mMediaList.insert(mPlayerIndex, mrl);
+           }
+       }
+       return ret;
+   }
+
+   public int expand(int index) {
+       mPlayerIndex = index;
+       return expand();
+   }
+}
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
index e818c72..1b95b67 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
@@ -37,6 +37,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Random;
 import java.util.Stack;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.videolan.libvlc.EventHandler;
 import org.videolan.libvlc.LibVLC;
@@ -44,6 +45,7 @@ import org.videolan.libvlc.LibVlcException;
 import org.videolan.libvlc.LibVlcUtil;
 import org.videolan.libvlc.Media;
 import org.videolan.libvlc.MediaList;
+import org.videolan.libvlc.MediaListPlayer;
 import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.R;
 import org.videolan.vlc.RemoteControlClientReceiver;
@@ -53,6 +55,7 @@ import org.videolan.vlc.gui.audio.AudioUtil;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
 import org.videolan.vlc.interfaces.IAudioService;
 import org.videolan.vlc.interfaces.IAudioServiceCallback;
+import org.videolan.vlc.interfaces.MediaParcelable;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.VLCInstance;
@@ -118,12 +121,14 @@ public class AudioService extends Service {
     public static final int NEXT_ITEM = 3;
 
     private LibVLC mLibVLC;
+    private MediaListPlayer mMediaListPlayer;
     private HashMap<IAudioServiceCallback, Integer> mCallback;
     private EventHandler mEventHandler;
     private OnAudioFocusChangeListener audioFocusListener;
     private boolean mDetectHeadset = true;
     private boolean mPebbleEnabled;
     private PowerManager.WakeLock mWakeLock;
+    private final AtomicBoolean mExpanding = new AtomicBoolean(false);
 
     private static boolean mWasPlayingAudio = false;
 
@@ -163,6 +168,7 @@ public class AudioService extends Service {
         } catch (LibVlcException e) {
             e.printStackTrace();
         }
+        mMediaListPlayer = new MediaListPlayer(mLibVLC);
 
         mCallback = new HashMap<IAudioServiceCallback, Integer>();
         mCurrentIndex = -1;
@@ -468,7 +474,7 @@ public class AudioService extends Service {
                     service.executeUpdate();
                     service.executeUpdateProgress();
 
-                    String location = service.mLibVLC.getMediaList().getMRL(service.mCurrentIndex);
+                    String location = service.mMediaListPlayer.getMediaList().getMRL(service.mCurrentIndex);
                     long length = service.mLibVLC.getLength();
                     MediaDatabase dbManager = MediaDatabase.getInstance();
                     Media m = dbManager.getMedia(location);
@@ -529,7 +535,7 @@ public class AudioService extends Service {
                 case EventHandler.MediaPlayerEncounteredError:
                     service.showToast(service.getString(
                         R.string.invalid_location,
-                        service.mLibVLC.getMediaList().getMRL(
+                        service.mMediaListPlayer.getMediaList().getMRL(
                                 service.mCurrentIndex)), Toast.LENGTH_SHORT);
                     service.executeUpdate();
                     service.executeUpdateProgress();
@@ -557,8 +563,6 @@ public class AudioService extends Service {
     private final Handler mListEventHandler = new MediaListEventHandler(this);
 
     private static class MediaListEventHandler extends WeakHandler<AudioService> {
-        // Don't clobber mCurrentIndex when MediaList is expanding itself.
-        boolean expanding = false;
 
         public MediaListEventHandler(AudioService audioService) {
             super(audioService);
@@ -574,7 +578,7 @@ public class AudioService extends Service {
             case EventHandler.CustomMediaListItemAdded:
                 Log.i(TAG, "CustomMediaListItemAdded");
                 index = msg.getData().getInt("item_index");
-                if(service.mCurrentIndex >= index && !expanding)
+                if(service.mCurrentIndex >= index && !service.mExpanding.get())
                     service.mCurrentIndex++;
 
                 service.determinePrevAndNextIndices();
@@ -583,20 +587,21 @@ public class AudioService extends Service {
             case EventHandler.CustomMediaListItemDeleted:
                 Log.i(TAG, "CustomMediaListItemDeleted");
                 index = msg.getData().getInt("item_index");
-                if (service.mCurrentIndex == index && !expanding) {
+                if (service.mCurrentIndex == index && !service.mExpanding.get()) {
                     // The current item has been deleted
                     service.mCurrentIndex--;
                     service.determinePrevAndNextIndices();
                     if (service.mNextIndex != -1)
                         service.next();
-                    else if (service.mCurrentIndex != -1)
-                        service.mLibVLC.playIndex(service.mCurrentIndex);
-                    else
+                    else if (service.mCurrentIndex != -1) {
+                        service.mMediaListPlayer.playIndex(service.mCurrentIndex);
+                        service.executeOnMediaPlayedAdded();
+                    } else
                         service.stop();
                     break;
                 }
 
-                if(service.mCurrentIndex > index && !expanding)
+                if(service.mCurrentIndex > index && !service.mExpanding.get())
                     service.mCurrentIndex--;
                 service.determinePrevAndNextIndices();
                 service.executeUpdate();
@@ -623,12 +628,6 @@ public class AudioService extends Service {
                 service.determinePrevAndNextIndices();
                 service.executeUpdate();
                 break;
-            case EventHandler.CustomMediaListExpanding:
-                expanding = true;
-                break;
-            case EventHandler.CustomMediaListExpandingEnd:
-                expanding = false;
-                break;
             }
         }
     };
@@ -638,7 +637,7 @@ public class AudioService extends Service {
             return;
         Log.i(TAG, "Obtained video track");
         String title = getCurrentMedia().getTitle();
-        String MRL = mLibVLC.getMediaList().getMRL(mCurrentIndex);
+        String MRL = mMediaListPlayer.getMediaList().getMRL(mCurrentIndex);
         int index = mCurrentIndex;
         mCurrentIndex = -1;
         mEventHandler.removeHandler(mVlcEventHandler);
@@ -692,7 +691,7 @@ public class AudioService extends Service {
      * @return The current media or null if there is not any.
      */
     private Media getCurrentMedia() {
-        return mLibVLC.getMediaList().getMedia(mCurrentIndex);
+        return mMediaListPlayer.getMediaList().getMedia(mCurrentIndex);
     }
 
     /**
@@ -701,7 +700,7 @@ public class AudioService extends Service {
      * @return True if a media is currently loaded, false otherwise
      */
     private boolean hasCurrentMedia() {
-        return mCurrentIndex >= 0 && mCurrentIndex < mLibVLC.getMediaList().size();
+        return mCurrentIndex >= 0 && mCurrentIndex < mMediaListPlayer.getMediaList().size();
     }
 
     private final Handler mHandler = new AudioServiceHandler(this);
@@ -857,7 +856,7 @@ public class AudioService extends Service {
     private void stop() {
         mLibVLC.stop();
         mEventHandler.removeHandler(mVlcEventHandler);
-        mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);
+        mMediaListPlayer.getMediaList().getEventHandler().removeHandler(mListEventHandler);
         setRemoteControlClientPlaybackState(EventHandler.MediaPlayerStopped);
         mCurrentIndex = -1;
         mPrevious.clear();
@@ -873,12 +872,18 @@ public class AudioService extends Service {
     }
 
     private void determinePrevAndNextIndices(boolean expand) {
-        mNextIndex = expand ? mLibVLC.expand() : -1;
+        if (expand) {
+            mExpanding.set(true);
+            mNextIndex = mMediaListPlayer.expand();
+            mExpanding.set(false);
+        } else {
+            mNextIndex = -1;
+        }
         mPrevIndex = -1;
 
         if (mNextIndex == -1) {
             // No subitems; play the next item.
-            int size = mLibVLC.getMediaList().size();
+            int size = mMediaListPlayer.getMediaList().size();
 
             // Repeating once doesn't change the index
             if (mRepeating == RepeatType.Once) {
@@ -928,14 +933,15 @@ public class AudioService extends Service {
         mPrevious.push(mCurrentIndex);
         mCurrentIndex = mNextIndex;
 
-        int size = mLibVLC.getMediaList().size();
+        int size = mMediaListPlayer.getMediaList().size();
         if (size == 0 || mCurrentIndex < 0 || mCurrentIndex >= size) {
             Log.w(TAG, "Warning: invalid next index, aborted !");
             stop();
             return;
         }
 
-        mLibVLC.playIndex(mCurrentIndex);
+        mMediaListPlayer.playIndex(mCurrentIndex);
+        executeOnMediaPlayedAdded();
 
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
         setUpRemoteControlClient();
@@ -988,14 +994,15 @@ public class AudioService extends Service {
         if (mPrevious.size() > 0)
             mPrevious.pop();
 
-        int size = mLibVLC.getMediaList().size();
+        int size = mMediaListPlayer.getMediaList().size();
         if (size == 0 || mPrevIndex < 0 || mCurrentIndex >= size) {
             Log.w(TAG, "Warning: invalid previous index, aborted !");
             stop();
             return;
         }
 
-        mLibVLC.playIndex(mCurrentIndex);
+        mMediaListPlayer.playIndex(mCurrentIndex);
+        executeOnMediaPlayedAdded();
         mHandler.sendEmptyMessage(SHOW_PROGRESS);
         setUpRemoteControlClient();
         showNotification();
@@ -1083,7 +1090,7 @@ public class AudioService extends Service {
         @Override
         public String getArtistPrev() throws RemoteException {
             if (mPrevIndex != -1)
-                return Util.getMediaArtist(AudioService.this, mLibVLC.getMediaList().getMedia(mPrevIndex));
+                return Util.getMediaArtist(AudioService.this, mMediaListPlayer.getMediaList().getMedia(mPrevIndex));
             else
                 return null;
         }
@@ -1091,7 +1098,7 @@ public class AudioService extends Service {
         @Override
         public String getArtistNext() throws RemoteException {
             if (mNextIndex != -1)
-                return Util.getMediaArtist(AudioService.this, mLibVLC.getMediaList().getMedia(mNextIndex));
+                return Util.getMediaArtist(AudioService.this, mMediaListPlayer.getMediaList().getMedia(mNextIndex));
             else
                 return null;
         }
@@ -1107,7 +1114,7 @@ public class AudioService extends Service {
         @Override
         public String getTitlePrev() throws RemoteException {
             if (mPrevIndex != -1)
-                return mLibVLC.getMediaList().getMedia(mPrevIndex).getTitle();
+                return mMediaListPlayer.getMediaList().getMedia(mPrevIndex).getTitle();
             else
                 return null;
         }
@@ -1115,7 +1122,7 @@ public class AudioService extends Service {
         @Override
         public String getTitleNext() throws RemoteException {
             if (mNextIndex != -1)
-                return mLibVLC.getMediaList().getMedia(mNextIndex).getTitle();
+                return mMediaListPlayer.getMediaList().getMedia(mNextIndex).getTitle();
             else
                 return null;
         }
@@ -1131,7 +1138,7 @@ public class AudioService extends Service {
         @Override
         public Bitmap getCoverPrev() throws RemoteException {
             if (mPrevIndex != -1)
-                return AudioUtil.getCover(AudioService.this, mLibVLC.getMediaList().getMedia(mPrevIndex), 64);
+                return AudioUtil.getCover(AudioService.this, mMediaListPlayer.getMediaList().getMedia(mPrevIndex), 64);
             else
                 return null;
         }
@@ -1139,7 +1146,7 @@ public class AudioService extends Service {
         @Override
         public Bitmap getCoverNext() throws RemoteException {
             if (mNextIndex != -1)
-                return AudioUtil.getCover(AudioService.this, mLibVLC.getMediaList().getMedia(mNextIndex), 64);
+                return AudioUtil.getCover(AudioService.this, mMediaListPlayer.getMediaList().getMedia(mNextIndex), 64);
             else
                 return null;
         }
@@ -1193,9 +1200,9 @@ public class AudioService extends Service {
             Log.v(TAG, "Loading position " + ((Integer)position).toString() + " in " + mediaPathList.toString());
             mEventHandler.addHandler(mVlcEventHandler);
 
-            mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);
-            mLibVLC.getMediaList().clear();
-            MediaList mediaList = mLibVLC.getMediaList();
+            mMediaListPlayer.getMediaList().getEventHandler().removeHandler(mListEventHandler);
+            mMediaListPlayer.getMediaList().clear();
+            MediaList mediaList = mMediaListPlayer.getMediaList();
 
             mPrevious.clear();
 
@@ -1217,11 +1224,11 @@ public class AudioService extends Service {
                 mediaList.add(media);
             }
 
-            if (mLibVLC.getMediaList().size() == 0) {
+            if (mMediaListPlayer.getMediaList().size() == 0) {
                 Log.w(TAG, "Warning: empty media list, nothing to play !");
                 return;
             }
-            if (mLibVLC.getMediaList().size() > position && position >= 0) {
+            if (mMediaListPlayer.getMediaList().size() > position && position >= 0) {
                 mCurrentIndex = position;
             } else {
                 Log.w(TAG, "Warning: positon " + position + " out of bounds");
@@ -1229,9 +1236,10 @@ public class AudioService extends Service {
             }
 
             // Add handler after loading the list
-            mLibVLC.getMediaList().getEventHandler().addHandler(mListEventHandler);
+            mMediaListPlayer.getMediaList().getEventHandler().addHandler(mListEventHandler);
 
-            mLibVLC.playIndex(mCurrentIndex);
+            mMediaListPlayer.playIndex(mCurrentIndex);
+            executeOnMediaPlayedAdded();
             mHandler.sendEmptyMessage(SHOW_PROGRESS);
             setUpRemoteControlClient();
             showNotification();
@@ -1249,11 +1257,11 @@ public class AudioService extends Service {
          */
         @Override
         public void playIndex(int index) {
-            if (mLibVLC.getMediaList().size() == 0) {
+            if (mMediaListPlayer.getMediaList().size() == 0) {
                 Log.w(TAG, "Warning: empty media list, nothing to play !");
                 return;
             }
-            if (index >= 0 && index < mLibVLC.getMediaList().size()) {
+            if (index >= 0 && index < mMediaListPlayer.getMediaList().size()) {
                 mCurrentIndex = index;
             } else {
                 Log.w(TAG, "Warning: index " + index + " out of bounds");
@@ -1261,7 +1269,8 @@ public class AudioService extends Service {
             }
 
             mEventHandler.addHandler(mVlcEventHandler);
-            mLibVLC.playIndex(mCurrentIndex);
+            mMediaListPlayer.playIndex(mCurrentIndex);
+            executeOnMediaPlayedAdded();
             mHandler.sendEmptyMessage(SHOW_PROGRESS);
             setUpRemoteControlClient();
             showNotification();
@@ -1278,7 +1287,7 @@ public class AudioService extends Service {
          */
         @Override
         public void showWithoutParse(int index) throws RemoteException {
-            String URI = mLibVLC.getMediaList().getMRL(index);
+            String URI = mMediaListPlayer.getMediaList().getMRL(index);
             Log.v(TAG, "Showing index " + index + " with playing URI " + URI);
             // Show an URI without interrupting/losing the current stream
 
@@ -1318,7 +1327,7 @@ public class AudioService extends Service {
                     Log.v(TAG, "Creating on-the-fly Media object for " + location);
                     media = new Media(mLibVLC, location);
                 }
-                mLibVLC.getMediaList().add(media);
+                mMediaListPlayer.getMediaList().add(media);
             }
             AudioService.this.saveMediaList();
             determinePrevAndNextIndices();
@@ -1330,34 +1339,43 @@ public class AudioService extends Service {
          */
         @Override
         public void moveItem(int positionStart, int positionEnd) throws RemoteException {
-            mLibVLC.getMediaList().move(positionStart, positionEnd);
+            mMediaListPlayer.getMediaList().move(positionStart, positionEnd);
             AudioService.this.saveMediaList();
         }
 
         @Override
         public void remove(int position) {
-            mLibVLC.getMediaList().remove(position);
+            mMediaListPlayer.getMediaList().remove(position);
             AudioService.this.saveMediaList();
         }
 
         @Override
         public void removeLocation(String location) {
-            mLibVLC.getMediaList().remove(location);
+            mMediaListPlayer.getMediaList().remove(location);
             AudioService.this.saveMediaList();
         }
 
         @Override
+        public List<MediaParcelable> getMedias() {
+            ArrayList<MediaParcelable> mp = new ArrayList<MediaParcelable>();
+            for (int i = 0; i < mMediaListPlayer.getMediaList().size(); i++) {
+                mp.add(new MediaParcelable(mMediaListPlayer.getMediaList().getMedia(i)));
+            }
+            return mp;
+        }
+
+        @Override
         public List<String> getMediaLocations() {
             ArrayList<String> medias = new ArrayList<String>();
-            for (int i = 0; i < mLibVLC.getMediaList().size(); i++) {
-                medias.add(mLibVLC.getMediaList().getMRL(i));
+            for (int i = 0; i < mMediaListPlayer.getMediaList().size(); i++) {
+                medias.add(mMediaListPlayer.getMediaList().getMRL(i));
             }
             return medias;
         }
 
         @Override
         public String getCurrentMediaLocation() throws RemoteException {
-            return mLibVLC.getMediaList().getMRL(mCurrentIndex);
+            return mMediaListPlayer.getMediaList().getMRL(mCurrentIndex);
         }
 
         @Override
@@ -1528,7 +1546,7 @@ public class AudioService extends Service {
         try {
             output = new FileOutputStream(AudioUtil.CACHE_DIR + "/" + "CurrentMedia.txt");
             bw = new BufferedWriter(new OutputStreamWriter(output));
-            bw.write(mLibVLC.getMediaList().getMRL(mCurrentIndex));
+            bw.write(mMediaListPlayer.getMediaList().getMRL(mCurrentIndex));
             bw.write('\n');
             bw.write(mShuffling ? "1" : "0");
             bw.write('\n');
@@ -1549,8 +1567,8 @@ public class AudioService extends Service {
         try {
             output = new FileOutputStream(AudioUtil.CACHE_DIR + "/" + "MediaList.txt");
             bw = new BufferedWriter(new OutputStreamWriter(output));
-            for (int i = 0; i < mLibVLC.getMediaList().size(); i++) {
-                bw.write(mLibVLC.getMediaList().getMRL(i));
+            for (int i = 0; i < mMediaListPlayer.getMediaList().size(); i++) {
+                bw.write(mMediaListPlayer.getMediaList().getMRL(i));
                 bw.write('\n');
             }
             bw.close();
diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
index b58961c..539a2af 100644
--- a/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
+++ b/vlc-android/src/org/videolan/vlc/audio/AudioServiceController.java
@@ -338,6 +338,11 @@ public class AudioServiceController implements IAudioPlayerControl {
     }
 
     @SuppressWarnings("unchecked")
+    public List<MediaParcelable> getMedias() {
+        return remoteProcedureCall(mAudioServiceBinder, List.class, null, "getMedias", null, null);
+    }
+
+    @SuppressWarnings("unchecked")
     public List<String> getMediaLocations() {
         List<String> def = new ArrayList<String>();
         return remoteProcedureCall(mAudioServiceBinder, List.class, def, "getMediaLocations", null, null);
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
index 38f84b1..f15759f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.java
@@ -20,12 +20,11 @@
 
 package org.videolan.vlc.gui.audio;
 
-import java.util.ArrayList;
+import java.util.List;
 
 import android.content.Context;
 import android.content.Intent;
 
-import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.Media;
 import org.videolan.vlc.R;
 import org.videolan.vlc.audio.AudioServiceController;
@@ -36,6 +35,7 @@ import org.videolan.vlc.gui.CommonDialogs.MenuType;
 import org.videolan.vlc.gui.audio.widget.CoverMediaSwitcher;
 import org.videolan.vlc.gui.audio.widget.HeaderMediaSwitcher;
 import org.videolan.vlc.interfaces.IAudioPlayer;
+import org.videolan.vlc.interfaces.MediaParcelable;
 import org.videolan.vlc.util.Strings;
 import org.videolan.vlc.util.Util;
 import org.videolan.vlc.widget.AudioMediaSwitcher.AudioMediaSwitcherListener;
@@ -386,23 +386,20 @@ public class AudioPlayer extends Fragment implements IAudioPlayer {
     }
 
     private void updateList() {
-        ArrayList<Media> audioList = new ArrayList<Media>();
-        String currentItem = null;
         int currentIndex = -1;
 
-        LibVLC libVLC = LibVLC.getExistingInstance();
-        for (int i = 0; i < libVLC.getMediaList().size(); i++) {
-            audioList.add(libVLC.getMediaList().getMedia(i));
-        }
-        currentItem = mAudioController.getCurrentMediaLocation();
-
         mSongsListAdapter.clear();
 
-        for (int i = 0; i < audioList.size(); i++) {
-            Media media = audioList.get(i);
-            if (currentItem != null && currentItem.equals(media.getLocation()))
-                currentIndex = i;
-            mSongsListAdapter.add(media);
+        final List<MediaParcelable> audioList = mAudioController.getMedias();
+        final String currentItem = mAudioController.getCurrentMediaLocation();
+
+        if (audioList != null) {
+            for (int i = 0; i < audioList.size(); i++) {
+                Media media = audioList.get(i).media;
+                if (currentItem != null && currentItem.equals(media.getLocation()))
+                    currentIndex = i;
+                mSongsListAdapter.add(media);
+            }
         }
         mSongsListAdapter.setCurrentIndex(currentIndex);
         mSongsList.setSelection(currentIndex);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 8e0c91a..6806669 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -44,6 +44,7 @@ import org.videolan.libvlc.LibVLC;
 import org.videolan.libvlc.LibVlcException;
 import org.videolan.libvlc.LibVlcUtil;
 import org.videolan.libvlc.Media;
+import org.videolan.libvlc.MediaListPlayer;
 import org.videolan.vlc.MediaDatabase;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
@@ -147,6 +148,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     private MediaRouter.SimpleCallback mMediaRouterCallback;
     private SecondaryDisplay mPresentation;
     private LibVLC mLibVLC;
+    private MediaListPlayer mMediaListPlayer;
     private String mLocation;
 
     private static final int SURFACE_BEST_FIT = 0;
@@ -371,6 +373,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             Log.d(TAG, "LibVLC initialisation failed");
             return;
         }
+        mMediaListPlayer = new MediaListPlayer(mLibVLC);
 
         mSurfaceView = (SurfaceView) findViewById(R.id.player_surface);
         mSurfaceHolder = mSurfaceView.getHolder();
@@ -1213,7 +1216,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
     }
 
     private void endReached() {
-        if(mLibVLC.getMediaList().expandMedia(savedIndexPosition) == 0) {
+        if(mMediaListPlayer.expand(savedIndexPosition) == 0) {
             Log.d(TAG, "Found a video playlist, expanding it");
             eventHandler.postDelayed(new Runnable() {
                 @Override
@@ -2321,7 +2324,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             savedIndexPosition = itemPosition;
             if(!mLibVLC.isPlaying()) {
                 // AudioService-transitioned playback for item after sleep and resume
-                mLibVLC.playIndex(savedIndexPosition);
+                mMediaListPlayer.playIndex(savedIndexPosition);
                 dontParse = false;
             }
             else {
@@ -2331,12 +2334,12 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
             updateNavStatus();
         } else if (savedIndexPosition > -1) {
             AudioServiceController.getInstance().stop(); // Stop the previous playback.
-            mLibVLC.playIndex(savedIndexPosition);
+            mMediaListPlayer.playIndex(savedIndexPosition);
         } else if (mLocation != null && mLocation.length() > 0 && !dontParse) {
             AudioServiceController.getInstance().stop(); // Stop the previous playback.
-            mLibVLC.getMediaList().add(new Media(mLibVLC, mLocation));
-            savedIndexPosition = mLibVLC.getMediaList().size() - 1;
-            mLibVLC.playIndex(savedIndexPosition);
+            mMediaListPlayer.getMediaList().add(new Media(mLibVLC, mLocation));
+            savedIndexPosition = mMediaListPlayer.getMediaList().size() - 1;
+            mMediaListPlayer.playIndex(savedIndexPosition);
         }
         mCanSeek = false;
 
diff --git a/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl b/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
index 24b556c..07cdaae 100644
--- a/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
+++ b/vlc-android/src/org/videolan/vlc/interfaces/IAudioService.aidl
@@ -20,6 +20,7 @@
 
 package org.videolan.vlc.interfaces;
 import org.videolan.vlc.interfaces.IAudioServiceCallback;
+import org.videolan.vlc.interfaces.MediaParcelable;
 
 interface IAudioService {
     void play();
@@ -34,6 +35,7 @@ interface IAudioService {
     void moveItem(int positionStart, int positionEnd);
     void remove(int position);
     void removeLocation(String location);
+    List<MediaParcelable> getMedias();
     List<String> getMediaLocations();
     String getCurrentMediaLocation();
     boolean isPlaying();
-- 
2.1.3



More information about the Android mailing list