From 7494ecafa2ddf835ca71d3bf2f7d76b478b55a49 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 22 Mar 2014 00:31:03 -0700 Subject: What appears to now be proper handling of reconnect --- .../dsub/service/ChromeCastController.java | 103 +++++++++++---------- 1 file changed, 52 insertions(+), 51 deletions(-) (limited to 'src/github') diff --git a/src/github/daneren2005/dsub/service/ChromeCastController.java b/src/github/daneren2005/dsub/service/ChromeCastController.java index 8e228047..a447260f 100644 --- a/src/github/daneren2005/dsub/service/ChromeCastController.java +++ b/src/github/daneren2005/dsub/service/ChromeCastController.java @@ -86,7 +86,7 @@ public class ChromeCastController extends RemoteController { castClientListener = new Cast.Listener() { @Override public void onApplicationStatusChanged() { - if (apiClient != null) { + if (apiClient != null && apiClient.isConnected()) { Log.i(TAG, "onApplicationStatusChanged: " + Cast.CastApi.getApplicationStatus(apiClient)); } } @@ -238,7 +238,7 @@ public class ChromeCastController extends RemoteController { mediaPlayer.stop(apiClient); } } catch(Exception e) { - Log.e(TAG, "Failed to stop RemoteMediaPlayer", e); + // Just means it didn't need to be stopped } downloadService.setPlayerState(PlayerState.IDLE); return; @@ -392,6 +392,7 @@ public class ChromeCastController extends RemoteController { @Override public void onConnected(Bundle connectionHint) { if (waitingForReconnect) { + Log.i(TAG, "Reconnecting"); reconnectApplication(); } else { launchApplication(); @@ -401,6 +402,8 @@ public class ChromeCastController extends RemoteController { @Override public void onConnectionSuspended(int cause) { Log.w(TAG, "Connection suspended"); + isPlaying = downloadService.getPlayerState() == PlayerState.STARTED; + position = getRemotePosition(); waitingForReconnect = true; } @@ -419,55 +422,52 @@ public class ChromeCastController extends RemoteController { } } void setupChannel() { - mediaPlayer = new RemoteMediaPlayer(); - mediaPlayer.setOnStatusUpdatedListener(new RemoteMediaPlayer.OnStatusUpdatedListener() { - @Override - public void onStatusUpdated() { - MediaStatus mediaStatus = mediaPlayer.getMediaStatus(); - if(mediaStatus == null) { - return; - } - - switch(mediaStatus.getPlayerState()) { - case MediaStatus.PLAYER_STATE_PLAYING: - if(ignoreNextPaused) { - ignoreNextPaused = false; - } - downloadService.setPlayerState(PlayerState.STARTED); - break; - case MediaStatus.PLAYER_STATE_PAUSED: - if(!ignoreNextPaused) { - downloadService.setPlayerState(PlayerState.PAUSED); - } - break; - case MediaStatus.PLAYER_STATE_BUFFERING: - downloadService.setPlayerState(PlayerState.PREPARING); - break; - case MediaStatus.PLAYER_STATE_IDLE: - if(mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED) { - downloadService.setPlayerState(PlayerState.COMPLETED); - downloadService.next(); - } else if(mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_INTERRUPTED) { + if(!waitingForReconnect) { + mediaPlayer = new RemoteMediaPlayer(); + mediaPlayer.setOnStatusUpdatedListener(new RemoteMediaPlayer.OnStatusUpdatedListener() { + @Override + public void onStatusUpdated() { + MediaStatus mediaStatus = mediaPlayer.getMediaStatus(); + if (mediaStatus == null) { + return; + } + + switch (mediaStatus.getPlayerState()) { + case MediaStatus.PLAYER_STATE_PLAYING: + if (ignoreNextPaused) { + ignoreNextPaused = false; + } + downloadService.setPlayerState(PlayerState.STARTED); + break; + case MediaStatus.PLAYER_STATE_PAUSED: + if (!ignoreNextPaused) { + downloadService.setPlayerState(PlayerState.PAUSED); + } + break; + case MediaStatus.PLAYER_STATE_BUFFERING: downloadService.setPlayerState(PlayerState.PREPARING); - } else if(mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_ERROR) { - Log.e(TAG, "Idle due to unknown error"); - downloadService.setPlayerState(PlayerState.COMPLETED); - downloadService.next(); - } else { - Log.w(TAG, "Idle reason: " + mediaStatus.getIdleReason()); - downloadService.setPlayerState(PlayerState.IDLE); - } - break; + break; + case MediaStatus.PLAYER_STATE_IDLE: + if (mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED) { + downloadService.setPlayerState(PlayerState.COMPLETED); + downloadService.next(); + } else if (mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_INTERRUPTED) { + if (downloadService.getPlayerState() != PlayerState.PREPARING) { + downloadService.setPlayerState(PlayerState.PREPARING); + } + } else if (mediaStatus.getIdleReason() == MediaStatus.IDLE_REASON_ERROR) { + Log.e(TAG, "Idle due to unknown error"); + downloadService.setPlayerState(PlayerState.COMPLETED); + downloadService.next(); + } else { + Log.w(TAG, "Idle reason: " + mediaStatus.getIdleReason()); + downloadService.setPlayerState(PlayerState.IDLE); + } + break; + } } - } - }); - mediaPlayer.setOnMetadataUpdatedListener(new RemoteMediaPlayer.OnMetadataUpdatedListener() { - @Override - public void onMetadataUpdated() { - MediaInfo mediaInfo = mediaPlayer.getMediaInfo(); - // TODO: Do I care about this? - } - }); + }); + } try { Cast.CastApi.setMessageReceivedCallbacks(apiClient, mediaPlayer.getNamespace(), mediaPlayer); @@ -475,10 +475,11 @@ public class ChromeCastController extends RemoteController { Log.e(TAG, "Exception while creating channel", e); } - DownloadFile currentPlaying = downloadService.getCurrentPlaying(); if(!waitingForReconnect) { + DownloadFile currentPlaying = downloadService.getCurrentPlaying(); startSong(currentPlaying, isPlaying, position); - } else { + } + if(waitingForReconnect) { waitingForReconnect = false; } } -- cgit v1.2.3