2024年8月6日发(作者:卫生间用什么做隔断最好)
// Show loading animation.
var playPromise = ();
if (playPromise !== undefined) {
(_ => {
// Automatic playback started!
// Show playing UI.
})
.catch(error => {
// Auto-play was prevented
// Show paused UI.
});
}
Example: Play & Pause
// Show loading animation.
var playPromise = ();
if (playPromise !== undefined) {
(_ => {
// Automatic playback started!
// Show playing UI.
// We can now safely
();
})
.catch(error => {
// Auto-play was prevented
// Show paused UI.
});
}
That's great for this simple example but what if you use
()
to be able to play a video later?
I'll tell you a secret. You don't have to use
()
, you can use
()
and here's how:
Example: Fetch & Play
ntListener('click', onButtonClick);
function onButtonClick() {
// This will allow us to play
();
fetchVideoAndPlay();
}
function fetchVideoAndPlay() {
fetch('/4')
.then(response => ())
.then(blob => {
ect = blob;
return ();
})
.then(_ => {
// Video playback started ;)
})
.catch(e => {
// Video playback failed ;(
})
}
Warning: Don't make your
onButtonClick
function asynchronous with the
async
keyword. You'll lose the "user gesture token" required to allow
your video to play later.
Play promise support
At the time of writing,
()
returns a promise in , Firefox, Opera, and . is still working on it.
Danger zone
within
makes
play()
promise never rejects
For
, the
play()
promise rejects as expected as the video doesn't exist. For
, the
play()
promise never rejects. It only happens if there are no valid sources.
更多推荐
卫生间,隔断,作者
发布评论