From 3386851d2a638892942a52ca8493458dfa683871 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 15 Sep 2015 08:55:45 -0700 Subject: Fix position not updating for remote playback anymore --- .../daneren2005/dsub/service/DownloadService.java | 52 +++++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index 0db091ad..3f3f036a 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -42,7 +42,6 @@ import github.daneren2005.dsub.domain.RepeatMode; import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.receiver.MediaButtonIntentReceiver; import github.daneren2005.dsub.util.ArtistRadioBuffer; -import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.Notifications; import github.daneren2005.dsub.util.SilentBackgroundTask; import github.daneren2005.dsub.util.Constants; @@ -1284,7 +1283,20 @@ public class DownloadService extends Service { } if(playerState == STARTED && positionCache == null && remoteState == LOCAL) { - positionCache = new PositionCache(); + positionCache = new LocalPositionCache(); + Thread thread = new Thread(positionCache, "PositionCache"); + thread.start(); + } else if(playerState != STARTED && positionCache != null) { + positionCache.stop(); + positionCache = null; + } + + if(playerState == STARTED && positionCache == null) { + if(remoteState == LOCAL) { + positionCache = new LocalPositionCache(); + } else { + positionCache = new PositionCache(); + } Thread thread = new Thread(positionCache, "PositionCache"); thread.start(); } else if(playerState != STARTED && positionCache != null) { @@ -1301,6 +1313,28 @@ public class DownloadService extends Service { isRunning = false; } + @Override + public void run() { + // Stop checking position before the song reaches completion + while(isRunning) { + try { + onSongProgress(); + Thread.sleep(1000L); + } + catch(Exception e) { + isRunning = false; + positionCache = null; + } + } + } + } + private class LocalPositionCache extends PositionCache { + boolean isRunning = true; + + public void stop() { + isRunning = false; + } + @Override public void run() { // Stop checking position before the song reaches completion @@ -2372,9 +2406,14 @@ public class DownloadService extends Service { } if(run) { - onSongsChanged(); - onSongProgress(); - onStateUpdate(); + mediaPlayerHandler.post(new Runnable() { + @Override + public void run() { + onSongsChanged(); + onSongProgress(); + onStateUpdate(); + } + }); } } public void removeOnSongChangeListener(OnSongChangedListener listener) { @@ -2418,12 +2457,13 @@ public class DownloadService extends Service { final long atRevision = revision; final Integer duration = getPlayerDuration(); final boolean isSeekable = isSeekable(); + final int position = getPlayerPosition(); for(final OnSongChangedListener listener: onSongChangedListeners) { handler.post(new Runnable() { @Override public void run() { if(revision == atRevision) { - listener.onSongProgress(currentPlaying, cachedPosition, duration, isSeekable); + listener.onSongProgress(currentPlaying, position, duration, isSeekable); } } }); -- cgit v1.2.3