[Android] AudioService: respond to list changes

Edward Wang git at videolan.org
Sun Sep 1 15:09:28 CEST 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Sun Sep  1 15:09:17 2013 +0200| [54905d233ac48fb457f654afe6b0105da8ad00d5] | committer: Edward Wang

AudioService: respond to list changes

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

 vlc-android/src/org/videolan/vlc/AudioService.java |   40 ++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/vlc-android/src/org/videolan/vlc/AudioService.java b/vlc-android/src/org/videolan/vlc/AudioService.java
index cda9aa9..ba47b02 100644
--- a/vlc-android/src/org/videolan/vlc/AudioService.java
+++ b/vlc-android/src/org/videolan/vlc/AudioService.java
@@ -458,6 +458,38 @@ public class AudioService extends Service {
         }
     };
 
+    private final Handler mListEventHandler = new MediaListEventHandler(this);
+
+    private static class MediaListEventHandler extends WeakHandler<AudioService> {
+        public MediaListEventHandler(AudioService audioService) {
+            super(audioService);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            AudioService service = getOwner();
+            if(service == null) return;
+
+            int index;
+            switch (msg.getData().getInt("event")) {
+            case EventHandler.MediaListItemAdded:
+                Log.i(TAG, "MediaListItemAdded");
+                index = msg.getData().getInt("item_index");
+                service.mMetadataCache.add(index, new Media(msg.getData().getString("item_uri"), false));
+                if(service.mCurrentIndex >= index)
+                    service.mCurrentIndex++;
+                break;
+            case EventHandler.MediaListItemDeleted:
+                Log.i(TAG, "MediaListItemDeleted");
+                index = msg.getData().getInt("item_index");
+                service.mMetadataCache.remove(index);
+                if(service.mCurrentIndex >= index)
+                    service.mCurrentIndex--;
+                break;
+            }
+        }
+    };
+
     private void handleVout() {
         Log.i(TAG, "Obtained video track");
         String title = getCurrentMedia().getTitle();
@@ -654,6 +686,7 @@ public class AudioService extends Service {
     private void stop() {
         mLibVLC.stop();
         mEventHandler.removeHandler(mVlcEventHandler);
+        mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);
         setRemoteControlClientPlaybackState(EventHandler.MediaPlayerStopped);
         mMetadataCache.clear();
         mCurrentIndex = -1;
@@ -666,7 +699,10 @@ public class AudioService extends Service {
 
     private void next() {
         // Try to expand any items present
+        mLibVLC.getMediaList().getEventHandler().removeHandler(mListEventHandler);
+        // Cache will be refreshed below
         int pos = mLibVLC.expandAndPlay();
+        mLibVLC.getMediaList().getEventHandler().addHandler(mListEventHandler);
         if(pos == 0) {
             Log.d(TAG, "Found subitems, updating media display");
             reloadMetadataCache();
@@ -896,6 +932,7 @@ 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.setMediaList();
             mLibVLC.getPrimaryMediaList().clear();
             MediaList mediaList = mLibVLC.getMediaList();
@@ -927,6 +964,9 @@ public class AudioService extends Service {
                 mCurrentIndex = 0;
             }
 
+            // Add handler after loading the list
+            mLibVLC.getMediaList().getEventHandler().addHandler(mListEventHandler);
+
             mLibVLC.playIndex(mCurrentIndex);
             mLibVLC.applyEqualizer();
             setUpRemoteControlClient();



More information about the Android mailing list