2
votes

After cast initialization, sender makes a request to load a media on running receiver

var session = window.cast.framework.CastContext.getInstance().getCurrentSession();
...
session.loadMedia(loadRequest);

Then, receiver loads next media using MediaManager

    var mediaManager = new cast.receiver.MediaManager(mediaElement);
    ...
    mediaManager.load(loadRequestData); // "Loads a media session on the receiver without involvement of a sender"

I need to update the media session on sender side. How do I achieve this?

2

2 Answers

2
votes

I was able to update media session on sender after receiver loads next media in the following way. For instance, in an episodic content, switching from one episode to another.

On Sender:

    var session = window.cast.framework.CastContext.getInstance().getCurrentSession();
    ...
    session.loadMedia(loadRequest).then(function() {
        ...
        // this is a media session of the first episodic content
        mediaSession = session.getMediaSession();   // returns instance of chrome.cast.media.Media
        mediaSession.addUpdateListener(function(isAlive) {
            // invoked when the status of the media has changed
            // [update next media session here]
        });
        ...
    }, function() {
       //errorHandler
    });

On Receiver

var mediaManager = new cast.receiver.MediaManager(video);
...
// the first episode is loaded from sender to receiver
...
// after the first episode is complete, loads second episode
mediaManager.load(loadRequestData); // loadRequestData contains media request data and its metadata

On Sender:

Add the following where it says [update next media session here] above.

var nextMediaSession = session.getSessionObj().media; //returns instance of chrome.cast.media.Media which is the updated media session of the second episode
0
votes

You can use the session.stop and addUpdateListener to end the current media when another one starts.

To stop, you can use this code snippet provided in the docs:

function stopApp() {
  session.stop(onSuccess, onError);
}

You can also check this related SO post for reference. Hope this helps.