From 8ac84a92099d5b6fb23a03b39ed9160f950ab8f5 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Wed, 19 Feb 2014 21:18:59 -0800 Subject: #281 Maintain state between switching from RemoteControllers --- .../dsub/service/ChromeCastController.java | 34 ++++++++++++------ .../daneren2005/dsub/service/DownloadService.java | 41 +++++++++++++--------- .../dsub/service/JukeboxController.java | 24 ++++++++----- .../daneren2005/dsub/service/RemoteController.java | 1 + 4 files changed, 64 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/service/ChromeCastController.java b/src/github/daneren2005/dsub/service/ChromeCastController.java index 591d23b9..0138e134 100644 --- a/src/github/daneren2005/dsub/service/ChromeCastController.java +++ b/src/github/daneren2005/dsub/service/ChromeCastController.java @@ -61,11 +61,15 @@ public class ChromeCastController extends RemoteController { private double gain = 0.5; public ChromeCastController(DownloadService downloadService, CastDevice castDevice) { - downloadService.setPlayerState(PlayerState.PREPARING); this.downloadService = downloadService; this.castDevice = castDevice; + } + + @Override + public void create(boolean playing, int seconds) { + downloadService.setPlayerState(PlayerState.PREPARING); - connectionCallbacks = new ConnectionCallbacks(); + connectionCallbacks = new ConnectionCallbacks(playing, seconds); connectionFailedListener = new ConnectionFailedListener(); castClientListener = new Cast.Listener() { @Override @@ -91,10 +95,10 @@ public class ChromeCastController extends RemoteController { Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(castDevice, castClientListener); apiClient = new GoogleApiClient.Builder(downloadService) - .addApi(Cast.API, apiOptionsBuilder.build()) - .addConnectionCallbacks(connectionCallbacks) - .addOnConnectionFailedListener(connectionFailedListener) - .build(); + .addApi(Cast.API, apiOptionsBuilder.build()) + .addConnectionCallbacks(connectionCallbacks) + .addOnConnectionFailedListener(connectionFailedListener) + .build(); apiClient.connect(); } @@ -104,7 +108,7 @@ public class ChromeCastController extends RemoteController { if(error) { error = false; Log.w(TAG, "Attempting to restart song"); - startSong(downloadService.getCurrentPlaying(), true); + startSong(downloadService.getCurrentPlaying(), true, 0); return; } @@ -167,7 +171,7 @@ public class ChromeCastController extends RemoteController { @Override public void changeTrack(int index, DownloadFile song) { - startSong(song, true); + startSong(song, true, 0); } @Override @@ -194,7 +198,7 @@ public class ChromeCastController extends RemoteController { } } - void startSong(DownloadFile currentPlaying, boolean autoStart) { + void startSong(DownloadFile currentPlaying, boolean autoStart, int position) { if(currentPlaying == null) { // Don't start anything return; @@ -239,7 +243,7 @@ public class ChromeCastController extends RemoteController { .setMetadata(meta) .build(); - mediaPlayer.load(apiClient, mediaInfo, autoStart).setResultCallback(new ResultCallback() { + mediaPlayer.load(apiClient, mediaInfo, autoStart, position * 1000L).setResultCallback(new ResultCallback() { @Override public void onResult(RemoteMediaPlayer.MediaChannelResult result) { if (result.getStatus().isSuccess()) { @@ -271,6 +275,14 @@ public class ChromeCastController extends RemoteController { private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks { + private boolean isPlaying; + private int position; + + ConnectionCallbacks(boolean isPlaying, int position) { + this.isPlaying = isPlaying; + this.position = position; + } + @Override public void onConnected(Bundle connectionHint) { if (waitingForReconnect) { @@ -351,7 +363,7 @@ public class ChromeCastController extends RemoteController { } DownloadFile currentPlaying = downloadService.getCurrentPlaying(); - startSong(currentPlaying, true); + startSong(currentPlaying, isPlaying, position); } } diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index f2383fdb..01753026 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -388,10 +388,7 @@ public class DownloadService extends Service { Util.sleepQuietly(50L); } - play(currentPlayingIndex, false); - if (currentPlaying != null && currentPlaying.isCompleteFileAvailable() && remoteState == RemoteControlState.LOCAL) { - doPlay(currentPlaying, currentPlayingPosition, autoPlayStart); - } + play(currentPlayingIndex, autoPlayStart, currentPlayingPosition); autoPlayStart = false; } @@ -723,15 +720,12 @@ public class DownloadService extends Service { nextPlayingTask = null; } setCurrentPlaying(index, start); - if (start) { - if (remoteState != RemoteControlState.LOCAL) { - remoteController.changeTrack(index, downloadList.get(index)); - setPlayerState(STARTED); - } else { - bufferAndPlay(position); - } + if (start && remoteState != RemoteControlState.LOCAL) { + remoteController.changeTrack(index, downloadList.get(index)); + setPlayerState(STARTED); } if (remoteState == RemoteControlState.LOCAL) { + bufferAndPlay(position, start); checkDownloads(); setNextPlaying(); } @@ -1113,6 +1107,9 @@ public class DownloadService extends Service { setRemoteState(newState, ref, null); } private void setRemoteState(final RemoteControlState newState, final Object ref, final String routeId) { + boolean isPlaying = playerState == STARTED; + int position = getPlayerPosition(); + if(remoteController != null) { remoteController.stop(); setPlayerState(PlayerState.IDLE); @@ -1126,7 +1123,6 @@ public class DownloadService extends Service { remoteController = new JukeboxController(this, handler); break; case CHROMECAST: - // TODO: Fix case where starting up with chromecast set if(ref == null) { remoteState = RemoteControlState.LOCAL; break; @@ -1137,6 +1133,12 @@ public class DownloadService extends Service { break; } + if(remoteController != null) { + remoteController.create(isPlaying, position / 1000); + } else { + play(getCurrentPlayingIndex(), isPlaying, position); + } + if (remoteState != RemoteControlState.LOCAL) { reset(); @@ -1187,13 +1189,16 @@ public class DownloadService extends Service { bufferAndPlay(0); } private synchronized void bufferAndPlay(int position) { + bufferAndPlay(position, true); + } + private synchronized void bufferAndPlay(int position, boolean start) { if(playerState != PREPARED) { reset(); - bufferTask = new BufferTask(currentPlaying, position); + bufferTask = new BufferTask(currentPlaying, position, start); bufferTask.start(); } else { - doPlay(currentPlaying, position, true); + doPlay(currentPlaying, position, start); } } @@ -1374,7 +1379,7 @@ public class DownloadService extends Service { } else { Log.i(TAG, "Requesting restart from " + pos + " of " + duration); reset(); - bufferTask = new BufferTask(downloadFile, pos); + bufferTask = new BufferTask(downloadFile, pos, true); bufferTask.start(); } } @@ -1630,11 +1635,13 @@ public class DownloadService extends Service { private final int position; private final long expectedFileSize; private final File partialFile; + private final boolean start; - public BufferTask(DownloadFile downloadFile, int position) { + public BufferTask(DownloadFile downloadFile, int position, boolean start) { this.downloadFile = downloadFile; this.position = position; partialFile = downloadFile.getPartialFile(); + this.start = start; // Calculate roughly how many bytes BUFFER_LENGTH_SECONDS corresponds to. int bitRate = downloadFile.getBitRate(); @@ -1655,7 +1662,7 @@ public class DownloadService extends Service { return; } } - doPlay(downloadFile, position, true); + doPlay(downloadFile, position, start); } private boolean bufferComplete() { diff --git a/src/github/daneren2005/dsub/service/JukeboxController.java b/src/github/daneren2005/dsub/service/JukeboxController.java index 2b8e7715..7f248f03 100644 --- a/src/github/daneren2005/dsub/service/JukeboxController.java +++ b/src/github/daneren2005/dsub/service/JukeboxController.java @@ -49,16 +49,24 @@ public class JukeboxController extends RemoteController { public JukeboxController(DownloadService downloadService, Handler handler) { this.downloadService = downloadService; this.handler = handler; - new Thread("JukeboxController") { - @Override - public void run() { - running = true; - processTasks(); - } - }.start(); - updatePlaylist(); } + @Override + public void create(boolean playing, int seconds) { + new Thread("JukeboxController") { + @Override + public void run() { + running = true; + processTasks(); + } + }.start(); + updatePlaylist(); + // Best I can do since API doesn't support seeking without starting playback + if(seconds != 0 && playing) { + changePosition(seconds); + } + } + @Override public void start() { tasks.remove(Stop.class); diff --git a/src/github/daneren2005/dsub/service/RemoteController.java b/src/github/daneren2005/dsub/service/RemoteController.java index ec50be51..17deec27 100644 --- a/src/github/daneren2005/dsub/service/RemoteController.java +++ b/src/github/daneren2005/dsub/service/RemoteController.java @@ -38,6 +38,7 @@ public abstract class RemoteController { protected DownloadService downloadService; private VolumeToast volumeToast; + public abstract void create(boolean playing, int seconds); public abstract void start(); public abstract void stop(); public abstract void shutdown(); -- cgit v1.2.3