1

The documentation for a web-based Chromecast sender says that castSession.loadMedia() returns a promise. Therefore, I should be able to catch errors in an async function like so:

const castSession = cast.framework.CastContext.getInstance().getCurrentSession();

const mediaInfo = new chrome.cast.media.MediaInfo(
  'https://example.com/stream.m3u8',
  'application/vnd.apple.mpegurl'
);

const loadRequest = new chrome.cast.media.LoadRequest(mediaInfo);

try {
  await castSession.getSessionObj().loadMedia(loadRequest);
} catch(e) {
  console.error(e);
}

Unfortunately, this doesn't work. Whatever load error I'm getting seems to occur sometime between the downloading of the HLS manifest and before playback. The loadMedia() promise resolves successfully, so there's no error to catch. The sender's developer console has an error:

cast_sender.js:85 Uncaught TypeError: d is not a function
    at cast_sender.js:85
    at V.onMessage (cast_sender.js:91)
    at S.h (cast_sender.js:71)
(anonymous) @ cast_sender.js:85
V.onMessage @ cast_sender.js:91
S.h @ cast_sender.js:71

Clearly, some sort of error is making it back to the client, and it seems that there should be some event handler that isn't set, but I don't see any documentation for how to catch the error.

How can I catch media load errors, general media errors, and any other errors on the Chromecast sender?

Brad
  • 159,648
  • 54
  • 349
  • 530

1 Answers1

2

chrome.cast.Session and cast.framework.CastSession are different. While CastSession methods returns promises, Session object works with callback. The error occurs because of missing callback when calling session.loadMedia()

In your case, you have to check docs of chrome.cast.Session object for further operation, or continue your work by calling directly loadMedia from your CastSession object:

Option 1: continue your work with CastSession object:

try {
  await castSession.loadMedia(loadRequest);
} catch(e) {
  console.error(e);
}

Option 2: Work with Session object

try {
  const sessionObj = await castSession.getSessionObj();
  await (new Promise((res) => {
    sessionObj.loadMedia(loadRequest, res);
  }));
} catch(e) {
  console.error(e);
}

More info:

https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session

Hoang Dao
  • 775
  • 5
  • 16
  • That worked! Thank you for the information. Could you please clarify when I should be using `cast.framework.CastSession` vs. when I should be using `chrome.cast.Session`? They seem similar, but for different purposes, and I'm unsure which I should be using in general. (That is, I understand the API difference now, but I'm curious if the difference between these two classes is just two different APIs for doing the same thing, or if they serve different purposes, and one just happens to utilize Promises.) Any info or documentation link explaining would be helpful! Thanks. – Brad Nov 16 '20 at 07:19
  • Sorry, I'm not too familiar with sender or cast. IMO, while cast objects are core components, `CastSession` is like a wrapper with facade pattern to improve the readability and usability to work with cast objects. You don't need to worry much about it since they're interchangeable in some ways, just use it to make the work done. – Hoang Dao Nov 16 '20 at 07:41
  • Ok, sounds good, thanks! I'll assign the point bounty as soon as Stack Overflow lets me. I really appreciate your help! Also, I've posted a couple other questions... if you're interested, I'd love to hear your input: https://stackoverflow.com/questions/64829471/get-chromecast-sender-url-on-receiver https://stackoverflow.com/questions/64839760/updating-chromecast-receiver-status-text-after-start – Brad Nov 16 '20 at 07:43