Skip to content

Commit

Permalink
Optimize html5 video ready check
Browse files Browse the repository at this point in the history
  • Loading branch information
yoriiis committed Feb 13, 2024
1 parent b6b65d6 commit 27e5d17
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/providers/html5/html5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,20 @@ export default function Html5Provider(Player: any) {
* @returns The video is ready
*/
waitUntilVideoIsReady(): Promise<any> {
return new window.Promise<void>((resolve) => resolve())
return new window.Promise<void>((resolve) => {
if (
(this.media.readyState >= 2 && this.media.duration) ||
(this.media.readyState === 0 && this.media.preload === 'none')
) {
resolve()
} else {
// Listen both events
// "loadedmetadata" is not fired on Firefox
// "canplay" is not fired in iOS
this.media.addEventListener('loadedmetadata', resolve, { once: true })
this.media.addEventListener('canplay', resolve, { once: true })
}
})
}

/**
Expand Down

0 comments on commit 27e5d17

Please sign in to comment.