Reputation: 886
I am using the cast reference player sample code to develop a receiver application. I am using the cast message bus to send a JSON string that will launch my media.
So in my player.html
, I init the cast message bus
. When I receive JSON
of the media that I want to play, I init player.js
from player.html
like so:
//receive message to play -> pass media through
var player = document.getElementById('player');
new sampleplayer.CastPlayer(player).start();
then in my player.js
:
sampleplayer.CastPlayer.prototype.start = function() {
var self = this;
var message = //JSON string
this.load(JSON.parse(message));
var millisecondsToWait = 8000;
setTimeout(function() {
//Pause Works
self.mediaElement_.pause();
}, millisecondsToWait);
var millisecondsToWait = 10000;
setTimeout(function() {
//Play Works
self.mediaElement_.play();
}, millisecondsToWait);
};
I am able to launch the media, I can play/ pause the media with the code above.
When I try use the play/ pause button on my remote control, I get the following error:
[cast.receiver.MediaManager] Unexpected command, player is in IDLE state so the media session ID is not valid yet
.
I also don't get any of the PlayState
updates that I was previously getting.
I believe I am not initialising something right, but I'm not sure what. Does anyone know of a good starting point for me? Thanks
Upvotes: 3
Views: 2414
Reputation: 1763
I hope this can help someone.
I could successfully load and start a video with the PlayerManger using playerManager.load(loadRequestData)
and playerManager.play()
. However, once I stopped using playerManager.stop()
and then tried to play again, I used to get this error:
[cast.receiver.MediaManager] Unexpected command, player is in IDLE state so the media session ID is not valid yet
This happens because the playerManager.stop()
method unloads the video from the tag.
To fix this behaviour, just check if a video is loaded before playing.
Example:
if (isNaN(playerManager.getDurationSec())) {
// the player has been stopped, then I reload the video
// Load with autoplay: playerManager.load(loadRequestData)
// create some queue items
const item = new cast.framework.messages.QueueItem()
item.media = new cast.framework.messages.MediaInformation()
item.media.contentId = '/movie.mp4'
const items = [item]
// Create a new queue with media.
let queueData = new cast.framework.messages.QueueData()
queueData.items = items
// [cast.receiver.MediaManager] Media or QueueData is mandatory
const loadRequestData = new cast.framework.messages.LoadRequestData()
loadRequestData.queueData = queueData
cast.framework.CastReceiverContext.getInstance()
.getPlayerManager().load(loadRequestData)
}
else {
playerManager.play()
}
Upvotes: 2