[error] The play() request was interrupted by a call to pause()
1
2
3
4
5
6
<video id="video" preload="none" src="https://example.com/file.mp4"></video>
<script>
video.play(); // <-- This is asynchronous!
video.pause();
</script>
Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().
video.play()
가 비동기 메서드기 때문에 비디오가 로드되기 전에 video.pause()
를 실행하면 위와 같은 에러가 발생한다.
video.play()
에 primise를 걸어 비디오가 로드된 후 pause 되도록 할 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<video id="video" preload="none" src="https://example.com/file.mp4"></video>
<script>
// Show loading animation.
const playPromise = video.play();
if (playPromise !== undefined) {
playPromise
.then((_) => {
// Automatic playback started!
// Show playing UI.
// We can now safely pause video...
video.pause();
})
.catch((error) => {
// Auto-play was prevented
// Show paused UI.
});
}
</script>
This post is licensed under CC BY 4.0 by the author.