Skip to content

Commit

Permalink
fix: prevent errors other than autoplay errors from muting video
Browse files Browse the repository at this point in the history
  • Loading branch information
robtpaton committed Aug 17, 2024
1 parent 2ead586 commit 607f0bb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions custom_components/webrtc/www/video-rtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class VideoRTC extends HTMLElement {

this.DISCONNECT_TIMEOUT = 5000;
this.RECONNECT_TIMEOUT = 30000;
this.PLAY_REATTEMPT_TIMEOUT = 500;

this.CODECS = [
'avc1.640029', // H.264 high 4.1 (Chromecast 1st and 2nd Gen)
Expand Down Expand Up @@ -163,12 +164,17 @@ export class VideoRTC extends HTMLElement {
* https://developer.chrome.com/blog/autoplay/
*/
play() {
this.video.play().catch(() => {
if (!this.video.muted) {
this.video.play().catch((firstError) => {
console.warn(firstError);
const isAutoplayError = firstError.message.includes("NotAllowedError");

if (!this.video.muted && isAutoplayError) {
this.video.muted = true;
this.video.play().catch(er => {
console.warn(er);
});
setTimeout(() =>
this.video.play().catch(secondError => {
console.warn(secondError);
}
), PLAY_REATTEMPT_TIMEOUT);
}
});
}
Expand Down

0 comments on commit 607f0bb

Please sign in to comment.