3

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

8
  • 1
    possible duplicate of stackoverflow.com/questions/35881391/…
    – Mr.Rebot
    Commented Apr 12, 2017 at 15:17
  • @Mr.Rebot, I saw that question alright, it is very similar, but I wasn't able to use the answer to my advantage.
    – mhorgan
    Commented Apr 12, 2017 at 15:28
  • If the question is the same, it's a duplicate. If you can, please elaborate on how the answer to the linked question does not solve your problem.
    – JAL
    Commented Apr 12, 2017 at 15:32
  • The answer linked assumes that I am going to queue items to play first via a sender application. I am trying to performing everything on the receiver side. There is no queueLoadItems on the cast receiver sdk, only the iOS sender sdk. i.e. linked answer needs a sender interaction, mine needs do it all on it's own.
    – mhorgan
    Commented Apr 12, 2017 at 15:39
  • @Mark117 Did you ever figure this out?
    – masterwok
    Commented Jun 12, 2017 at 2:32

1 Answer 1

2

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()
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.