diff --git a/src/android/ChromecastSession.java b/src/android/ChromecastSession.java index 05d7173..ead184a 100644 --- a/src/android/ChromecastSession.java +++ b/src/android/ChromecastSession.java @@ -13,6 +13,7 @@ import com.google.android.gms.cast.MediaLoadRequestData; import com.google.android.gms.cast.MediaMetadata; import com.google.android.gms.cast.MediaSeekOptions; +import com.google.android.gms.cast.MediaStatus; import com.google.android.gms.cast.TextTrackStyle; import com.google.android.gms.cast.framework.CastSession; import com.google.android.gms.cast.framework.media.RemoteMediaClient; @@ -70,78 +71,74 @@ public void run() { return; } client.registerCallback(new RemoteMediaClient.Callback() { - private String currentMedia = ""; + private String currentState = "idle"; @Override public void onStatusUpdated() { - super.onStatusUpdated(); + MediaStatus status = client.getMediaStatus(); + if (status != null) { + switch (status.getPlayerState()) { + case MediaStatus.PLAYER_STATE_LOADING: + case MediaStatus.PLAYER_STATE_IDLE: + if (!currentState.equals("requesting")) { + currentState = "loading"; + } + break; + default: + if (currentState.equals("loading")) { + clientListener.onMediaLoaded(createMediaObject()); + } + currentState = "loaded"; + break; + } + } clientListener.onMediaUpdate(createMediaObject()); } @Override public void onMetadataUpdated() { - super.onMetadataUpdated(); - MediaInfo info = client.getMediaInfo(); - if (info == null) { - currentMedia = ""; - } else { - String newMedia = info.getContentId(); - if (!currentMedia.equals(newMedia)) { - currentMedia = newMedia; - clientListener.onMediaLoaded(createMediaObject()); - } - } clientListener.onMediaUpdate(createMediaObject()); } @Override public void onQueueStatusUpdated() { - super.onQueueStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } @Override public void onPreloadStatusUpdated() { - super.onPreloadStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } @Override public void onSendingRemoteMediaRequest() { - super.onSendingRemoteMediaRequest(); + currentState = "requesting"; clientListener.onMediaUpdate(createMediaObject()); } @Override public void onAdBreakStatusUpdated() { - super.onAdBreakStatusUpdated(); clientListener.onMediaUpdate(createMediaObject()); } }); session.addCastListener(new Cast.Listener() { @Override public void onApplicationStatusChanged() { - super.onApplicationStatusChanged(); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onApplicationMetadataChanged(ApplicationMetadata appMetadata) { - super.onApplicationMetadataChanged(appMetadata); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onApplicationDisconnected(int i) { - super.onApplicationDisconnected(i); clientListener.onSessionEnd( ChromecastUtilities.createSessionObject(session, "stopped")); } @Override public void onActiveInputStateChanged(int i) { - super.onActiveInputStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onStandbyStateChanged(int i) { - super.onStandbyStateChanged(i); clientListener.onSessionUpdate(createSessionObject()); } @Override public void onVolumeChanged() { - super.onVolumeChanged(); clientListener.onSessionUpdate(createSessionObject()); } }); diff --git a/src/android/ChromecastUtilities.java b/src/android/ChromecastUtilities.java index c5296f4..3d443c2 100644 --- a/src/android/ChromecastUtilities.java +++ b/src/android/ChromecastUtilities.java @@ -44,6 +44,7 @@ static String getMediaIdleReason(MediaStatus mediaStatus) { static String getMediaPlayerState(MediaStatus mediaStatus) { switch (mediaStatus.getPlayerState()) { + case MediaStatus.PLAYER_STATE_LOADING: case MediaStatus.PLAYER_STATE_BUFFERING: return "BUFFERING"; case MediaStatus.PLAYER_STATE_IDLE: @@ -187,7 +188,6 @@ static TextTrackStyle parseTextTrackStyle(JSONObject textTrackSytle) { out.setForegroundColor(Color.parseColor(textTrackSytle.getString("foregroundColor"))); } } catch (JSONException e) { - e.printStackTrace(); } return out; @@ -203,7 +203,6 @@ static JSONObject createSessionObject(CastSession session, String state) { try { s.put("status", state); } catch (JSONException e) { - } } return s; @@ -221,9 +220,8 @@ static JSONObject createSessionObject(CastSession session) { out.put("sessionId", session.getSessionId()); } catch (JSONException e) { - e.printStackTrace(); } catch (NullPointerException e) { - + } catch (IllegalStateException e) { } return out; @@ -240,7 +238,6 @@ private static JSONArray createAppImagesObject(CastSession session) { } } } catch (NullPointerException e) { - } return appImages; } @@ -256,14 +253,11 @@ private static JSONObject createReceiverObject(CastSession session) { volume.put("level", session.getVolume()); volume.put("muted", session.isMute()); } catch (JSONException e) { - } out.put("volume", volume); } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; } @@ -312,9 +306,7 @@ static JSONObject createMediaObject(CastSession session) { } } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -350,9 +342,7 @@ private static JSONArray createMediaInfoTracks(CastSession session) { out.put(jsonTrack); } } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -383,9 +373,7 @@ private static JSONObject createMediaInfoObject(CastSession session) { // TODO: Check if it's useful //out.put("metadata", mediaInfo.getMetadata()); } catch (JSONException e) { - } catch (NullPointerException e) { - } return out; @@ -407,7 +395,6 @@ static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) { out.put("windowRoundedCornerRadius", textTrackStyle.getWindowCornerRadius()); out.put("windowType", getWindowType(textTrackStyle)); } catch (JSONException e) { - } return out; @@ -445,7 +432,6 @@ static JSONObject createError(String code, String message) { out.put("code", code); out.put("description", message); } catch (JSONException e) { - } return out; }