Skip to content

Commit

Permalink
fix(player): Added missing try/catch blocks to execute exceptions pro…
Browse files Browse the repository at this point in the history
…perly
  • Loading branch information
rafa8626 committed Dec 23, 2019
1 parent 07ca4bf commit ecbb9fd
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/js/utils/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,34 +96,50 @@ export function isAutoplaySupported(media: HTMLMediaElement, autoplay: (n: any)
playPromise.then(() => {
// Umuted autoplay works.
media.pause();
autoplay(true);
// Autoplay with sound not will be working for IOS, macOS
// Apple has strict policy about that
muted((IS_IOS || IS_SAFARI));
callback();
try {
autoplay(true);
// Autoplay with sound not will be working for IOS, macOS
// Apple has strict policy about that
muted((IS_IOS || IS_SAFARI));
callback();
} catch (e) {
throw new Error('Attempt to autoplay with sound failed');
}
}).catch(() => {
// Unmuted autoplay failed. New attempt with muted autoplay.
media.volume = 0;
media.muted = true;
media.play().then(() => {
// Muted autoplay works.
media.pause();
autoplay(true);
muted(true);
callback();
try {
autoplay(true);
muted(true);
callback();
} catch (e) {
throw new Error('Attempt to autoplay without sound failed');
}
}).catch(() => {
// Both muted and unmuted autoplay failed. Fallback to click to play.
media.volume = 1;
media.muted = false;
autoplay(false);
muted(false);
callback();
try {
autoplay(false);
muted(false);
callback();
} catch (e) {
throw new Error('Cannot autoplay');
}
});
});
} else {
autoplay(!media.paused || 'Promise' in window && playPromise instanceof Promise);
media.pause();
muted(false);
callback();
try {
autoplay(!media.paused || 'Promise' in window && playPromise instanceof Promise);
media.pause();
muted(false);
callback();
} catch (e) {
throw new Error('Attempt to autoplay failed');
}
}
}

0 comments on commit ecbb9fd

Please sign in to comment.