Skip to content

Commit

Permalink
Only update session.addMediaListnener (MEDIA_LOADED event) when an ex…
Browse files Browse the repository at this point in the history
…ternal sender loads media.

Handle PLAYER_STATE_LOADING, returning buffering is better than unknown.
Remove calls to super because they do nothing.
session.getApplicationMetadata() throws IllegalStateException if called before connected, catch it like the others.
  • Loading branch information
Lindsay-Needs-Sleep committed Sep 29, 2019
1 parent a04f0e4 commit 6408db0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 39 deletions.
43 changes: 20 additions & 23 deletions src/android/ChromecastSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
});
Expand Down
18 changes: 2 additions & 16 deletions src/android/ChromecastUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -187,7 +188,6 @@ static TextTrackStyle parseTextTrackStyle(JSONObject textTrackSytle) {
out.setForegroundColor(Color.parseColor(textTrackSytle.getString("foregroundColor")));
}
} catch (JSONException e) {
e.printStackTrace();
}

return out;
Expand All @@ -203,7 +203,6 @@ static JSONObject createSessionObject(CastSession session, String state) {
try {
s.put("status", state);
} catch (JSONException e) {

}
}
return s;
Expand All @@ -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;
Expand All @@ -240,7 +238,6 @@ private static JSONArray createAppImagesObject(CastSession session) {
}
}
} catch (NullPointerException e) {

}
return appImages;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -312,9 +306,7 @@ static JSONObject createMediaObject(CastSession session) {
}

} catch (JSONException e) {

} catch (NullPointerException e) {

}

return out;
Expand Down Expand Up @@ -350,9 +342,7 @@ private static JSONArray createMediaInfoTracks(CastSession session) {
out.put(jsonTrack);
}
} catch (JSONException e) {

} catch (NullPointerException e) {

}

return out;
Expand Down Expand Up @@ -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;
Expand All @@ -407,7 +395,6 @@ static JSONObject createTextTrackObject(TextTrackStyle textTrackStyle) {
out.put("windowRoundedCornerRadius", textTrackStyle.getWindowCornerRadius());
out.put("windowType", getWindowType(textTrackStyle));
} catch (JSONException e) {

}

return out;
Expand Down Expand Up @@ -445,7 +432,6 @@ static JSONObject createError(String code, String message) {
out.put("code", code);
out.put("description", message);
} catch (JSONException e) {

}
return out;
}
Expand Down

0 comments on commit 6408db0

Please sign in to comment.