diff options
author | Scott Jackson <daneren2005@gmail.com> | 2013-03-23 18:14:43 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2013-03-23 18:14:43 -0700 |
commit | 0953126a63feb8a3946bcb436a6bd17eeddb0cfa (patch) | |
tree | a3db977244661a03679c9026337f24f777b8228d /subsonic-android/src/github | |
parent | d04e9845a096a6c052c93688d6f250e6f60b4599 (diff) | |
download | dsub-0953126a63feb8a3946bcb436a6bd17eeddb0cfa.tar.gz dsub-0953126a63feb8a3946bcb436a6bd17eeddb0cfa.tar.bz2 dsub-0953126a63feb8a3946bcb436a6bd17eeddb0cfa.zip |
Moved position caching to a native thread
Diffstat (limited to 'subsonic-android/src/github')
-rw-r--r-- | subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java index 114edd1d..bf68e905 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java @@ -120,7 +120,7 @@ public class DownloadServiceImpl extends Service implements DownloadService { private VisualizerController visualizerController; private boolean showVisualization; private boolean jukeboxEnabled; - private ScheduledExecutorService executorService; + private PositionCache positionCache; private StreamProxy proxy; private Timer sleepTimer; @@ -836,27 +836,38 @@ public class DownloadServiceImpl extends Service implements DownloadService { scrobbler.scrobble(this, currentPlaying, true); } - if(playerState == STARTED && (executorService == null || executorService.isShutdown())) { - Runnable runnable = new Runnable() { - @Override - public void run() { + if(playerState == STARTED && positionCache == null) { + positionCache = new PositionCache(); + Thread thread = new Thread(positionCache); + thread.start(); + } else if(playerState != STARTED && positionCache != null) { + positionCache.stop(); + positionCache = null; + } + } + + private class PositionCache implements Runnable { + boolean isRunning = true; + + public void stop() { + isRunning = false; + } + + @Override + public void run() { + while(isRunning) { + try { if(mediaPlayer != null && playerState == STARTED) { - try { - cachedPosition = mediaPlayer.getCurrentPosition(); - } catch(Exception e) { - executorService.shutdown(); - } + cachedPosition = mediaPlayer.getCurrentPosition(); } + Thread.sleep(200L); + } + catch(Exception e) { + isRunning = false; } - }; - executorService = Executors.newSingleThreadScheduledExecutor(); - executorService.scheduleWithFixedDelay(runnable, 200L, 200L, TimeUnit.MILLISECONDS); - } else if(playerState != STARTED) { - if(executorService != null && !executorService.isShutdown()) { - executorService.shutdownNow(); } } - } + } private synchronized void setPlayerStateCompleted() { Log.i(TAG, this.playerState.name() + " -> " + PlayerState.COMPLETED + " (" + currentPlaying + ")"); |