From 2ba267cb2103389270703274ca26792071438b00 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Tue, 3 Jul 2012 12:11:46 +0200 Subject: [PATCH] Playbackservice now switches to next item in queue when completed --- .../activity/MediaplayerActivity.java | 31 +++++++++++++------ src/de/podfetcher/feed/FeedManager.java | 8 ++++- .../podfetcher/service/PlaybackService.java | 19 ++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/de/podfetcher/activity/MediaplayerActivity.java b/src/de/podfetcher/activity/MediaplayerActivity.java index 3d09bafba..c7638b2df 100644 --- a/src/de/podfetcher/activity/MediaplayerActivity.java +++ b/src/de/podfetcher/activity/MediaplayerActivity.java @@ -546,6 +546,15 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements * sbPosition.getMax()); } break; + case PlaybackService.NOTIFICATION_TYPE_RELOAD: + unbindService(mConnection); + if (positionObserver != null) { + positionObserver.cancel(true); + positionObserver = null; + } + mediaInfoLoaded = false; + bindToService(); + break; } } else { @@ -573,18 +582,20 @@ public class MediaplayerActivity extends SherlockFragmentActivity implements protected Void doInBackground(MediaPlayer... p) { Log.d(TAG, "Background Task started"); player = p[0]; + try { + while (player.isPlaying() && !isCancelled()) { + try { + Thread.sleep(WAITING_INTERVALL); + } catch (InterruptedException e) { + Log.d(TAG, + "Thread was interrupted while waiting. Finishing now"); + return null; + } + publishProgress(); - while (player.isPlaying() && !isCancelled()) { - try { - Thread.sleep(WAITING_INTERVALL); - } catch (InterruptedException e) { - Log.d(TAG, - "Thread was interrupted while waiting. Finishing now"); - return null; - } catch (IllegalStateException e) { - Log.d(TAG, "player is in illegal state, exiting now"); } - publishProgress(); + } catch (IllegalStateException e) { + Log.d(TAG, "player is in illegal state, exiting now"); } Log.d(TAG, "Background Task finished"); return null; diff --git a/src/de/podfetcher/feed/FeedManager.java b/src/de/podfetcher/feed/FeedManager.java index ecf7990cf..9e61bbd6a 100644 --- a/src/de/podfetcher/feed/FeedManager.java +++ b/src/de/podfetcher/feed/FeedManager.java @@ -209,7 +209,9 @@ public class FeedManager { boolean removed = queue.remove(item); if (removed) { PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); adapter.setQueue(queue); + adapter.close(); } } @@ -218,7 +220,11 @@ public class FeedManager { } public FeedItem getFirstQueueItem() { - return queue.get(0); + if (queue.isEmpty()) { + return null; + } else { + return queue.get(0); + } } private void addNewFeed(Context context, Feed feed) { diff --git a/src/de/podfetcher/service/PlaybackService.java b/src/de/podfetcher/service/PlaybackService.java index 07a24a2b1..bf7cc90e9 100644 --- a/src/de/podfetcher/service/PlaybackService.java +++ b/src/de/podfetcher/service/PlaybackService.java @@ -27,6 +27,7 @@ import android.os.IBinder; import de.podfetcher.PodcastApp; import de.podfetcher.activity.MediaplayerActivity; +import de.podfetcher.feed.FeedItem; import de.podfetcher.feed.FeedMedia; import de.podfetcher.feed.Feed; import de.podfetcher.feed.FeedManager; @@ -64,6 +65,7 @@ public class PlaybackService extends Service { public static final int NOTIFICATION_TYPE_ERROR = 0; public static final int NOTIFICATION_TYPE_INFO = 1; public static final int NOTIFICATION_TYPE_BUFFER_UPDATE = 2; + public static final int NOTIFICATION_TYPE_RELOAD = 3; /** Is true if service is running. */ @@ -382,8 +384,21 @@ public class PlaybackService extends Service { manager.removeQueueItem(PlaybackService.this, media.getItem()); } manager.setFeedMedia(PlaybackService.this, media); - setStatus(PlayerStatus.STOPPED); - stopForeground(true); + + FeedItem nextItem = manager.getFirstQueueItem(); + if (nextItem == null) { + Log.d(TAG, "No more items in queue"); + setStatus(PlayerStatus.STOPPED); + stopForeground(true); + } else { + Log.d(TAG, "Loading next item in queue"); + media = nextItem.getMedia(); + feed = nextItem.getFeed(); + shouldStream = !media.isDownloaded(); + resetVideoSurface(); + sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); + } + } };