forked from TencentX5/x5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
play-after-timeout.htm
44 lines (37 loc) · 1.34 KB
/
play-after-timeout.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>setTimeout 后调用播放</title>
</head>
<body>
<video id="video" controls preload x5-video-player-type="h5-page" height="50%" width="100%" type="video/mp4" src="">
<!-- <source src="http://cdn.tencent.neigou.com/Public/Home/mobileAsset/images/tencent2018/video5.mp4" type="video/mp4"> -->
</video>
<div><button id="button">播放</button></div>
<script src="https://libs.cdnjs.net/vConsole/3.3.4/vconsole.min.js"></script>
<script>
new VConsole();
const video = document.getElementById('video');
const button = document.getElementById('button');
const delay = t => new Promise(resolve => setTimeout(resolve, t));
const onClick = async () => {
video.src = './empty.mp4';
video.load();
await delay(500);
video.src = 'http://cdn.tencent.neigou.com/Public/Home/mobileAsset/images/tencent2018/video5.mp4';
video.play();
};
const onCanplaythrough = () => {
video.play().then(() => {
console.log('播放成功');
}).catch(() => {
console.error('播放被阻止');
});
};
button.addEventListener('click', onClick);
video.addEventListener('canplaythrough', onCanplaythrough);
</script>
</body>
</html>