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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>video.js - roc</title> <link rel="stylesheet" href="https://unpkg.com/video.js@8.10.0/dist/video-js.min.css" /> </head> <body> <div class="wrap"> <div style="width: 1024px"> <video style="width: 100%" class="video-js" id="my-player"></video> </div> <div> <p>视频总长: <span id="duration"></span></p> </div> <div style="margin-top: 10px"> <button onclick="player.play()">播放</button> <button onclick="player.pause()">暂停</button> <button onclick="player.currentTime(player.currentTime() + 5)">快进5秒</button> <button onclick="player.currentTime(player.currentTime() - 5)">后退5秒</button> <button onclick="player.volume(player.volume() + 0.05)">加大音量(+5)</button> <button onclick="player.volume(player.volume() - 0.05)">减小音量(-5)</button> <button onclick="player.volume(0.5)">设置音量50</button> <button onclick="player.muted(true)">静音</button> <button onclick="player.muted(false)">关闭静音</button> <button onclick="changePlayerM3u8()">切换m3u8</button> <button onclick="changePlayerMp4()">切换mp4</button> <button onclick="player.requestFullscreen()">全屏</button> <button onclick="player.exitFullscreen()">退出全屏</button> <button onclick="player.load(); player.play()">重新加载</button> <button onclick="player.dispose()">销毁(会删除video标签)</button> </div> </div> <script src="https://unpkg.com/video.js@8.10.0/dist/video.min.js"></script> <script src="https://unpkg.com/video.js@8.10.0/dist/lang/zh-CN.js"></script>
<script>
const player = videojs( "my-player", { language: "zh-CN", aspectRatio: "16:9", controls: true, autoplay: false, muted: false, loop: false, poster: "https://rocyuan666.gitee.io/img/rocyuan.jpg", preload: "auto", sources: [ { src: "https://rocyuan666.github.io/assets/%E3%80%90%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%99%A8%E3%80%91code-generator-VAR/1.mp4", type: "video/mp4", }, ], }, function () { videojs.log("播放器准备完成~");
console.log(this.muted());
this.on("error", (error) => { videojs.log("播放错误", error); });
this.on("stalled", () => { videojs.log("网速异常"); });
this.on("play", () => { videojs.log("开始播放"); });
this.on("playing", () => { videojs.log("不论什么原因(刚开始播放、转跳后播放等...场景)每次播放都会触发一次,播放中..."); });
this.on("pause", () => { videojs.log("暂停播放"); });
this.on("ended", () => { videojs.log("播放结束"); });
this.on("seeking", () => { videojs.log("视频跳转中"); });
this.on("seeked", () => { videojs.log("视频跳转结束"); });
this.on("ratechange", () => { videojs.log("播放速率改变"); });
this.on("timeupdate", () => { videojs.log("播放时间变化", this.currentTime()); });
this.on("volumechange", () => { const volume = Number((this.volume() * 100).toFixed(0)); videojs.log("播放音量变化(包括静音设置)", volume); videojs.log("是否静音", this.muted()); });
this.on("fullscreenchange", () => { videojs.log("是否全屏", this.isFullscreen()); });
this.on("loadstart", () => { videojs.log("开始请求数据"); });
this.on("loadedmetadata", () => { videojs.log("获取资源长度完成 "); document.getElementById("duration").innerHTML = player.duration() + " 秒"; });
this.on("progress", () => { videojs.log("正在请求数据..."); });
this.on("canplaythrough", () => { videojs.log("视频源数据加载完成"); });
this.on("waiting", () => { videojs.log("等待数据"); });
} );
function changePlayerM3u8() { player.src([ { src: "https://rocyuan666.github.io/assets/%E3%80%90%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%99%A8%E3%80%91code-generator-VAR/1.m3u8", type: "application/x-mpegURL", }, ]); player.play(); }
function changePlayerMp4() { player.src([ { src: "https://rocyuan666.github.io/assets/%E3%80%90%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90%E5%99%A8%E3%80%91code-generator-VAR/1.mp4", type: "video/mp4", }, ]); player.play(); } </script> </body> </html>
|