Skip to content

Commit

Permalink
fix(player): log warning when autoplay fails first time
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Oct 1, 2023
1 parent a16e644 commit 666103b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/vidstack/src/core/state/media-player-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { DOMEvent, type InferEventDetail, type InferEventInit } from 'maverick.j
import type { MediaContext } from '../api/media-context';
import type { MediaEvents } from '../api/media-events';

let seenAutoplayWarning = false;

export class MediaPlayerDelegate {
constructor(private _handle: (event: Event) => void, private _media: MediaContext) {}
constructor(
private _handle: (event: Event) => void,
private _media: MediaContext,
) {}

_dispatch<Type extends keyof MediaEvents>(
type: Type,
Expand Down Expand Up @@ -56,6 +61,20 @@ export class MediaPlayerDelegate {
await player.play();
this._dispatch('autoplay', { detail: { muted: $state.muted() } });
} catch (error) {
if (__DEV__ && !seenAutoplayWarning) {
const muteMsg = !$state.muted()
? ' Attempting with volume muted will most likely resolve the issue.'
: '';
console.warn(
[
`[vidstack]: autoplay was requested but failed most likely due to browser autoplay policies.${muteMsg}`,
`\nSee https://developer.chrome.com/blog/autoplay`,
`\nError:\n\n${error}`,
].join('\n'),
);
seenAutoplayWarning = true;
}

this._dispatch('autoplay-fail', {
detail: {
muted: $state.muted(),
Expand Down
2 changes: 1 addition & 1 deletion packages/vidstack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (__DEV__) {
console.warn('[vidstack] dev mode!');
console.warn('[vidstack]: dev mode!');
}

// Foundation
Expand Down

0 comments on commit 666103b

Please sign in to comment.