Skip to content

Commit

Permalink
Merge pull request #36 from Vaporexpress/Fix-mediaUpdate-isAlive
Browse files Browse the repository at this point in the history
Fix several bugs (duplicate load media, media listeners bug, etc)
  • Loading branch information
matbee-eth committed Dec 23, 2014
2 parents 94a2986 + 825de48 commit 642db97
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
17 changes: 10 additions & 7 deletions chrome.cast.js
Expand Up @@ -528,7 +528,7 @@ chrome.cast.requestSession = function (successCallback, errorCallback, opt_sessi

var session = _sessions[sessionId] = new chrome.cast.Session(sessionId, appId, displayName, appImages, receiver);
successCallback(session);
_sessionListener(session);
/*_sessionListener(session); Fix - Already has a sessionListener*/
} else {
handleError(err, errorCallback);
}
Expand Down Expand Up @@ -707,6 +707,8 @@ chrome.cast.Session.prototype.loadMedia = function (loadRequest, successCallback
if (!err) {
_currentMedia = new chrome.cast.media.Media(self.sessionId, obj.mediaSessionId);
_currentMedia.media = mediaInfo;
_currentMedia.media.duration = obj.media.duration;
_currentMedia.currentTime = obj.currentTime /* ??? */

// TODO: Fill in the rest of the media properties

Expand Down Expand Up @@ -993,9 +995,10 @@ chrome.cast.media.Media.prototype.removeUpdateListener = function (listener) {
this.removeListener('_mediaUpdated', listener);
};

chrome.cast.media.Media.prototype._update = function(obj) {
chrome.cast.media.Media.prototype._update = function(isAlive, obj) {
this.currentTime = obj.currentTime || this.currentTime;
this.idleReason = obj.idleReason || this.idleReason;
this.sessionId = obj.sessionId || this.sessionId;
this.mediaSessionId = obj.mediaSessionId || this.mediaSessionId;
this.playbackRate = obj.playbackRate || this.playbackRate;
this.playerState = obj.playerState || this.playerState;
Expand All @@ -1010,7 +1013,7 @@ chrome.cast.media.Media.prototype._update = function(obj) {

this._lastUpdatedTime = Date.now();

this.emit('_mediaUpdated');
this.emit('_mediaUpdated', isAlive);
};


Expand Down Expand Up @@ -1097,16 +1100,16 @@ chrome.cast._ = {
_sessions[session.sessionId]._update(isAlive, session);
}
},
mediaUpdated: function(media) {
mediaUpdated: function(isAlive, media) {
if (media && media.mediaSessionId !== undefined && _currentMedia) {
_currentMedia._update(media);
_currentMedia._update(isAlive, media);
}
},
mediaLoaded: function(media) {
mediaLoaded: function(isAlive, media) {
if (_sessions[media.sessionId]) {
console.log('mediaLoaded');
_currentMedia = new chrome.cast.media.Media(media.sessionId, media.mediaSessionId);
_currentMedia._update(media);
_currentMedia._update(isAlive, media);

_sessions[media.sessionId].emit('_mediaListener', _currentMedia);
} else {
Expand Down
12 changes: 8 additions & 4 deletions src/android/Chromecast.java
Expand Up @@ -734,9 +734,13 @@ private JSONObject routeToJSON(RouteInfo route) {
}

@Override
public void onMediaUpdated(JSONObject media) {
this.webView.sendJavascript("chrome.cast._.mediaUpdated(" + media.toString() +");");
}
public void onMediaUpdated(boolean isAlive, JSONObject media) {
if (isAlive) {
this.webView.sendJavascript("chrome.cast._.mediaUpdated(true, " + media.toString() +");");
} else {
this.webView.sendJavascript("chrome.cast._.mediaUpdated(false, " + media.toString() +");");
}
}

@Override
public void onSessionUpdated(boolean isAlive, JSONObject session) {
Expand All @@ -751,7 +755,7 @@ public void onSessionUpdated(boolean isAlive, JSONObject session) {

@Override
public void onMediaLoaded(JSONObject media) {
this.webView.sendJavascript("chrome.cast._.mediaLoaded(" + media.toString() +");");
this.webView.sendJavascript("chrome.cast._.mediaLoaded(true, " + media.toString() +");");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/android/ChromecastOnMediaUpdatedListener.java
Expand Up @@ -4,5 +4,5 @@

public interface ChromecastOnMediaUpdatedListener {
void onMediaLoaded(JSONObject media);
void onMediaUpdated(JSONObject media);
void onMediaUpdated(boolean isAlive, JSONObject media);
}
8 changes: 5 additions & 3 deletions src/android/ChromecastSession.java
Expand Up @@ -207,6 +207,7 @@ public void onResult(MediaChannelResult result) {
if (result.getStatus().isSuccess()) {
System.out.println("Media loaded successfully");

ChromecastSession.this.onMediaUpdatedListener.onMediaLoaded(ChromecastSession.this.createMediaObject());
callback.onSuccess(ChromecastSession.this.createMediaObject());

} else {
Expand Down Expand Up @@ -427,7 +428,8 @@ public void onResult(ApplicationConnectionResult result) {
@Override
public void onResult(MediaChannelResult result) {
if (result.getStatus().isSuccess()) {
ChromecastSession.this.onMediaUpdatedListener.onMediaLoaded(ChromecastSession.this.createMediaObject());
ChromecastSession.this.onMediaUpdatedListener.onMediaUpdated(true, ChromecastSession.this.createMediaObject());
/*ChromecastSession.this.onMediaUpdatedListener.onMediaLoaded(ChromecastSession.this.createMediaObject());*/
} else {
System.out.println("Failed to request status.");
}
Expand Down Expand Up @@ -634,15 +636,15 @@ public void onApplicationDisconnected(int errorCode) {
@Override
public void onMetadataUpdated() {
if (this.onMediaUpdatedListener != null) {
this.onMediaUpdatedListener.onMediaUpdated(this.createMediaObject());
this.onMediaUpdatedListener.onMediaUpdated(true, this.createMediaObject());
}
}


@Override
public void onStatusUpdated() {
if (this.onMediaUpdatedListener != null) {
this.onMediaUpdatedListener.onMediaUpdated(this.createMediaObject());
this.onMediaUpdatedListener.onMediaUpdated(true, this.createMediaObject());
}
}

Expand Down

0 comments on commit 642db97

Please sign in to comment.