VideoPlayBox.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <script setup>
  2. import { nextTick, reactive,ref,onMounted, computed } from "vue-demi"
  3. import {debounce} from 'lodash'
  4. /**
  5. * 格式化时间
  6. * @param {number} e 秒
  7. * @returns string eg:03:27
  8. */
  9. function formatTime(e){
  10. const h=parseInt(e/3600)
  11. const m=parseInt(e/60%60)
  12. const s=parseInt(e%60)
  13. return `${h>0?h>9?h+':':'0'+h+':':''}${m>9?m:'0'+m}:${s>9?s:'0'+s}`
  14. }
  15. /**
  16. * 是否全屏
  17. * @returns boolean
  18. */
  19. function isFullScreen(){
  20. return document.webkitIsFullScreen || document.fullscreen
  21. }
  22. /**
  23. * 设置全屏
  24. */
  25. function setFullScreen(el){
  26. if (el.requestFullscreen) {
  27. el.requestFullscreen()
  28. return true
  29. } else if (el.msRequestFullscreen) {
  30. el.msRequestFullscreen()
  31. return true
  32. } else if (el.mozRequestFullScreen) {
  33. el.mozRequestFullScreen()
  34. return true
  35. } else if (el.webkitRequestFullScreen) {
  36. el.webkitRequestFullScreen()
  37. return true
  38. }
  39. return false
  40. }
  41. /**
  42. * 关闭全屏
  43. */
  44. function closeFullScreen(){
  45. document.exitFullscreen() || document.webkitExitFullScreen() || document.msExitFullscreen() || document.mozCancelFullScreen()
  46. }
  47. //监听页面全屏变化
  48. function listenFullScreen(){
  49. document.addEventListener('fullscreenchange',()=>{
  50. videoState.isFullScreen=isFullScreen()
  51. })
  52. }
  53. const props=defineProps({
  54. videoUrl:{
  55. type:String,
  56. require:true
  57. },
  58. speedOpt:{
  59. type:Array,
  60. default:['2.0','1.5','1.25','1.0','0.8','0.5']
  61. }
  62. })
  63. const videoIns=ref(null)//视频实例
  64. let videoWrap=ref(null)//视频盒子
  65. let preloadSlider=computed(()=>{//缓冲的进度
  66. return (videoState.cacheTime/videoState.duration)*100+'%'
  67. })
  68. let activeSlider=computed(()=>{//当前播放的进度
  69. return (videoState.time/videoState.duration)*100+'%'
  70. })
  71. //视频状态
  72. let videoState=reactive({
  73. play:false,
  74. isPageFullScreen:false,//网页全屏
  75. isFullScreen:false,//是否全屏
  76. duration:0,//视频时长
  77. time:0,//当前播放时长
  78. cacheTime:0,//当前缓冲到的时间
  79. speed:'1.0',
  80. speedOpt:props.speedOpt,
  81. volume:100,
  82. isHover:false
  83. })
  84. const emit=defineEmits(['play','pause','timeupdate'])
  85. //视频事件
  86. function videoPlay(e){
  87. videoState.play=true
  88. emit('play',e)
  89. }
  90. function videoCanPlay(e){
  91. const target=e.target
  92. videoState.duration=target.duration
  93. }
  94. function videoTimeUpdate(e){
  95. const target=e.target
  96. videoState.time=target.currentTime
  97. videoState.duration=target.duration
  98. emit('timeupdate')
  99. }
  100. function videoPause(e){
  101. videoState.play=false
  102. emit('pause',e)
  103. }
  104. function videoProgress(e){
  105. const target=e.target
  106. videoState.cacheTime=target.buffered.length && target.buffered.end(target.buffered.length - 1)
  107. }
  108. // 点击暂停\播放视频
  109. function handleChangeVideoPlay(){
  110. if(videoState.play){
  111. videoIns.value.pause()
  112. }else{
  113. videoIns.value.play()
  114. }
  115. }
  116. //切换倍速
  117. function handleChangeVideoSpeed(item){
  118. if(videoState.speed==item) return
  119. videoState.speed=item
  120. videoIns.value.playbackRate=Number(item)
  121. }
  122. // 音量变化
  123. function handleChangeVideoVolume(){
  124. videoIns.value.volume=videoState.volume/100
  125. }
  126. //一键静音
  127. function handleVideoMute(){
  128. videoState.volume=0
  129. videoIns.value.volume=0
  130. }
  131. //拖动进度条
  132. function handleDragVideoSlider(val){
  133. console.log(val);
  134. videoIns.value.currentTime=val
  135. }
  136. //点击切换全屏
  137. function handleChangeVideoScreen(){
  138. // 如果网页全屏为true
  139. if(videoState.isPageFullScreen){
  140. videoState.isPageFullScreen=false
  141. return
  142. }
  143. if(!isFullScreen()){
  144. setFullScreen(videoWrap.value)
  145. setTimeout(() => {
  146. // 无法设置全屏 则设置为网页全屏
  147. if(!isFullScreen()){
  148. videoState.isPageFullScreen=true
  149. }
  150. }, 100);
  151. }else{
  152. closeFullScreen()
  153. }
  154. }
  155. // 鼠标进入视频区域
  156. function mouseMoveVideoWrap(){
  157. videoState.isHover=true
  158. hideVideoControlBox()
  159. }
  160. //隐藏控制器
  161. const hideVideoControlBox=debounce(()=>{
  162. videoState.isHover=false
  163. },3000)
  164. onMounted(() => {
  165. listenFullScreen()
  166. })
  167. </script>
  168. <template>
  169. <div
  170. ref="videoWrap"
  171. :class="[
  172. 'video-wrap',
  173. videoState.isPageFullScreen?'page-fullscreen':''
  174. ]"
  175. @contextmenu="e=>e.preventDefault()"
  176. @mousemove="mouseMoveVideoWrap"
  177. >
  178. <video
  179. class="video"
  180. ref="videoIns"
  181. :src="videoUrl"
  182. autoplay
  183. @canplay="videoCanPlay"
  184. @play="videoPlay"
  185. @pause="videoPause"
  186. @timeupdate="videoTimeUpdate"
  187. @progress="videoProgress"
  188. @click="handleChangeVideoPlay"
  189. :controls="false"
  190. x5-playsinline
  191. raw-controls
  192. controls360=no
  193. playsinline
  194. webkit-playsinline
  195. x-webkit-airplay="allow"
  196. x5-video-player-type="h5"
  197. x5-video-player-fullscreen
  198. x5-video-orientation="portraint"
  199. >
  200. <span>您的浏览器不支持 video 标签。</span>
  201. </video>
  202. <!-- 视频控制器模块 -->
  203. <div class="video-control-box" v-show="videoState.isHover||!videoState.play">
  204. <!-- 进度条 -->
  205. <div class="video-progress-slider-box" >
  206. <!-- 缓冲进度条 -->
  207. <div class="preload-slider" :style="{width:preloadSlider}"></div>
  208. <!-- 实时进度 -->
  209. <el-slider
  210. class="active-slider"
  211. size="small"
  212. v-model="videoState.time"
  213. :max="videoState.duration"
  214. :show-tooltip="false"
  215. @input="handleDragVideoSlider"
  216. />
  217. </div>
  218. <div class="top-btn-box">
  219. <div style="display:flex;align-items: center;">
  220. <div class="play-btn hover-btn" @click.stop="handleChangeVideoPlay">
  221. <svg v-if="!videoState.play" t="1668758429272" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2674" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  222. <path d="M870.2 466.333333l-618.666667-373.28a53.333333 53.333333 0 0 0-80.866666 45.666667v746.56a53.206667 53.206667 0 0 0 80.886666 45.666667l618.666667-373.28a53.333333 53.333333 0 0 0 0-91.333334z" fill="#ffffff" p-id="2675" data-spm-anchor-id="a313x.7781069.0.i0" class="selected"></path>
  223. </svg>
  224. <svg v-else t="1668760616711" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3627" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  225. <path d="M428.539658 833.494155c0 15.954367-13.053294 29.007661-29.007661 29.007661L285.613458 862.501816c-15.954367 0-29.007661-13.053294-29.007661-29.007661l0-639.423111c0-15.954367 13.053294-29.007661 29.007661-29.007661l113.918539 0c15.954367 0 29.007661 13.053294 29.007661 29.007661L428.539658 833.494155z" p-id="3628" data-spm-anchor-id="a313x.7781069.0.i3" class="selected" fill="#ffffff"></path><path d="M760.124635 833.494155c0 15.954367-13.053294 29.007661-29.007661 29.007661l-113.918539 0c-15.954367 0-29.007661-13.053294-29.007661-29.007661l0-639.423111c0-15.954367 13.053294-29.007661 29.007661-29.007661l113.918539 0c15.954367 0 29.007661 13.053294 29.007661 29.007661L760.124635 833.494155z" p-id="3629" data-spm-anchor-id="a313x.7781069.0.i4" class="selected" fill="#ffffff"></path>
  226. </svg>
  227. </div>
  228. <span class="video-time">{{formatTime(videoState.time)}}/{{formatTime(videoState.duration)}}</span>
  229. </div>
  230. <div style="display:flex;align-items: center;">
  231. <!-- 声音大小 -->
  232. <div class="volume-btn">
  233. <svg v-if="videoState.volume==0" t="1669010015626" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3708" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  234. <path d="M128 420.576v200.864h149.12l175.456 140.064V284.288l-169.792 136.288H128z m132.256-64l204.288-163.968a32 32 0 0 1 52.032 24.96v610.432a32 32 0 0 1-51.968 24.992l-209.92-167.552H96a32 32 0 0 1-32-32v-264.864a32 32 0 0 1 32-32h164.256zM752 458.656L870.4 300.8a32 32 0 1 1 51.2 38.4L792 512l129.6 172.8a32 32 0 0 1-51.2 38.4l-118.4-157.856-118.4 157.856a32 32 0 0 1-51.2-38.4l129.6-172.8-129.6-172.8a32 32 0 0 1 51.2-38.4l118.4 157.856z" p-id="3709" data-spm-anchor-id="a313x.7781069.0.i3" class="selected" fill="#ffffff"></path>
  235. </svg>
  236. <svg v-else @click.stop="handleVideoMute" t="1669010104115" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6297" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  237. <path d="M257.493333 322.4l215.573334-133.056c24.981333-15.413333 57.877333-7.914667 73.493333 16.746667 5.301333 8.373333 8.106667 18.048 8.106667 27.914666v555.989334C554.666667 819.093333 530.784 842.666667 501.333333 842.666667c-9.994667 0-19.786667-2.773333-28.266666-8L257.493333 701.6H160c-41.237333 0-74.666667-33.013333-74.666667-73.738667V396.138667c0-40.725333 33.429333-73.738667 74.666667-73.738667h97.493333z m26.133334 58.4a32.298667 32.298667 0 0 1-16.96 4.8H160c-5.888 0-10.666667 4.714667-10.666667 10.538667v231.733333c0 5.813333 4.778667 10.538667 10.666667 10.538667h106.666667c5.994667 0 11.872 1.664 16.96 4.8L490.666667 770.986667V253.013333L283.626667 380.8zM800.906667 829.653333a32.288 32.288 0 0 1-45.248-0.757333 31.317333 31.317333 0 0 1 0.768-44.693333c157.653333-150.464 157.653333-393.962667 0-544.426667a31.317333 31.317333 0 0 1-0.768-44.682667 32.288 32.288 0 0 1 45.248-0.757333c183.68 175.306667 183.68 460.010667 0 635.317333z m-106.901334-126.186666a32.288 32.288 0 0 1-45.248-1.216 31.328 31.328 0 0 1 1.237334-44.672c86.229333-80.608 86.229333-210.56 0-291.178667a31.328 31.328 0 0 1-1.237334-44.672 32.288 32.288 0 0 1 45.248-1.216c112.885333 105.546667 112.885333 277.418667 0 382.965333z" p-id="6298" data-spm-anchor-id="a313x.7781069.0.i5" class="selected" fill="#ffffff"></path>
  238. </svg>
  239. <div class="volume-slider-box">
  240. <el-slider v-model="videoState.volume" vertical height="100px" @input="handleChangeVideoVolume" />
  241. </div>
  242. </div>
  243. <!-- 倍速 -->
  244. <div class="speed-btn">
  245. <span class="num">{{videoState.speed}}X</span>
  246. <div class="speed-opt-box">
  247. <span
  248. :class="['item',videoState.speed==item&&'active']"
  249. v-for="item in videoState.speedOpt"
  250. :key="item"
  251. @click.stop="handleChangeVideoSpeed(item)"
  252. >{{item}}x</span>
  253. </div>
  254. </div>
  255. <!-- 全屏按钮 -->
  256. <div class="fullpage-screen-btn hover-btn" @click.stop="handleChangeVideoScreen">
  257. <svg v-if="videoState.isPageFullScreen||videoState.isFullScreen" t="1668999840019" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1951" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  258. <path d="M298.666667 0c-25.6 0-42.666667 17.066667-42.666667 42.666667v192c0 12.8-8.533333 21.333333-21.333333 21.333333H42.666667c-25.6 0-42.666667 17.066667-42.666667 42.666667s17.066667 42.666667 42.666667 42.666666h192C294.4 341.333333 341.333333 294.4 341.333333 234.666667V42.666667c0-25.6-17.066667-42.666667-42.666666-42.666667zM789.333333 341.333333H981.333333c25.6 0 42.666667-17.066667 42.666667-42.666666s-17.066667-42.666667-42.666667-42.666667h-192c-12.8 0-21.333333-8.533333-21.333333-21.333333V42.666667c0-25.6-17.066667-42.666667-42.666667-42.666667s-42.666667 17.066667-42.666666 42.666667v192C682.666667 294.4 729.6 341.333333 789.333333 341.333333zM234.666667 682.666667H42.666667c-25.6 0-42.666667 17.066667-42.666667 42.666666s17.066667 42.666667 42.666667 42.666667h192c12.8 0 21.333333 8.533333 21.333333 21.333333V981.333333c0 25.6 17.066667 42.666667 42.666667 42.666667s42.666667-17.066667 42.666666-42.666667v-192C341.333333 729.6 294.4 682.666667 234.666667 682.666667zM981.333333 682.666667h-192c-59.733333 0-106.666667 46.933333-106.666666 106.666666V981.333333c0 25.6 17.066667 42.666667 42.666666 42.666667s42.666667-17.066667 42.666667-42.666667v-192c0-12.8 8.533333-21.333333 21.333333-21.333333H981.333333c25.6 0 42.666667-17.066667 42.666667-42.666667s-17.066667-42.666667-42.666667-42.666666z" p-id="1952" data-spm-anchor-id="a313x.7781069.0.i0" class="selected" fill="#ffffff"></path>
  259. </svg>
  260. <svg v-else t="1668762444009" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2682" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200">
  261. <path d="M170.666667 170.666667v213.333333H85.333333V85.333333h298.666667v85.333334H170.666667z m682.666666 213.333333V170.666667h-213.333333V85.333333h298.666667v298.666667h-85.333334zM170.666667 640v213.333333h213.333333v85.333334H85.333333v-298.666667h85.333334z m682.666666 0h85.333334v298.666667h-298.666667v-85.333334h213.333333v-213.333333z" p-id="2683" data-spm-anchor-id="a313x.7781069.0.i0" class="selected" fill="#ffffff"></path>
  262. </svg>
  263. </div>
  264. </div>
  265. </div>
  266. </div>
  267. <!-- 弹幕画布 -->
  268. <canvas class="barrage-box"></canvas>
  269. </div>
  270. </template>
  271. <style lang="scss" scoped>
  272. // 清除浮动
  273. .clear-float::after {
  274. content: "";
  275. height: 0;
  276. line-height: 0;
  277. display: block;
  278. visibility: hidden;
  279. clear: both;
  280. }
  281. .video-wrap{
  282. position: relative;
  283. width: 100%;
  284. height: 100%;
  285. overflow: hidden;
  286. background-color: rgba(0,0,0,1);
  287. .video{
  288. width: 100%;
  289. height: 100%;
  290. object-fit: contain;
  291. display: block;
  292. box-sizing: border-box;
  293. }
  294. .video-control-box{
  295. position: absolute;
  296. z-index: 5;
  297. left: 0;
  298. right: 0;
  299. bottom: 0;
  300. .top-btn-box{
  301. padding: 5px;
  302. background-color: rgba(0,0,0,0.7);
  303. display: flex;
  304. align-items: center;
  305. justify-content: space-between;
  306. position: relative;
  307. z-index: 10;
  308. .hover-btn{
  309. cursor: pointer;
  310. &:hover{
  311. background-color: #303031;
  312. }
  313. }
  314. .play-btn{
  315. width: 40px;
  316. height: 40px;
  317. padding-left: 10px;
  318. padding-top: 10px;
  319. border-radius: 50%;
  320. .icon{
  321. width: 20px;
  322. height: 20px;
  323. }
  324. }
  325. .video-time{
  326. color: #fff;
  327. font-size: 14px;
  328. display: inline-block;
  329. margin-left: 10px;
  330. }
  331. .fullpage-screen-btn{
  332. width: 40px;
  333. height: 40px;
  334. padding-left: 10px;
  335. padding-top: 10px;
  336. border-radius: 50%;
  337. .icon{
  338. width: 20px;
  339. height: 20px;
  340. }
  341. }
  342. .speed-btn{
  343. color: #fff;
  344. font-size: 13px;
  345. margin-right: 10px;
  346. cursor: pointer;
  347. display: inline-block;
  348. padding: 3px 8px;
  349. border-radius: 20px;
  350. position: relative;
  351. &:hover{
  352. background-color: #303031;
  353. .speed-opt-box{
  354. display: block;
  355. }
  356. }
  357. .speed-opt-box{
  358. display: none;
  359. position: absolute;
  360. width: 80px;
  361. bottom: 105%;
  362. left: 50%;
  363. transform: translateX(-50%);
  364. background-color: #303031;
  365. color: #fff;
  366. font-size: 14px;
  367. border-radius: 2px;
  368. padding: 5px 0;
  369. .item{
  370. display: block;
  371. text-align: center;
  372. padding: 5px 0;
  373. &.active{
  374. color: #F3A52F;
  375. }
  376. }
  377. }
  378. }
  379. .volume-btn{
  380. width: 40px;
  381. height: 40px;
  382. padding-left: 10px;
  383. padding-top: 10px;
  384. border-radius: 50%;
  385. cursor: pointer;
  386. margin-right: 10px;
  387. position: relative;
  388. &:hover{
  389. .volume-slider-box{
  390. display: block;
  391. }
  392. }
  393. .icon{
  394. width: 20px;
  395. height: 20px;
  396. }
  397. .volume-slider-box{
  398. display: none;
  399. position: absolute;
  400. bottom: 100%;
  401. left: 50%;
  402. transform: translateX(-50%);
  403. }
  404. }
  405. }
  406. .video-progress-slider-box{
  407. position: relative;
  408. width: 100%;
  409. height: 3px;
  410. background-color: #303031;
  411. cursor: pointer;
  412. .preload-slider{
  413. position: absolute;
  414. height: 100%;
  415. background-color: #717171;
  416. left: 0;
  417. z-index: 1;
  418. }
  419. .active-slider{
  420. position: absolute;
  421. height: 100%;
  422. left: 0;
  423. z-index: 15;
  424. --el-slider-height:3px;
  425. --el-slider-button-wrapper-size:18px;
  426. --el-slider-button-size:16px;
  427. --el-slider-button-wrapper-offset:-12px;
  428. :deep(.el-slider__button-wrapper){
  429. display: none;
  430. }
  431. :deep(.el-slider__runway){
  432. background-color: transparent;
  433. &:hover{
  434. .el-slider__button-wrapper{
  435. display: block;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. }
  442. .barrage-box{
  443. width: 100%;
  444. height: 100%;
  445. position: absolute;
  446. left: 0;
  447. top: 0;
  448. right: 0;
  449. bottom: 0;
  450. pointer-events: none;
  451. z-index: 10;
  452. }
  453. }
  454. .page-fullscreen{
  455. position: fixed;
  456. left: 0;
  457. top: 0;
  458. right: 0;
  459. bottom: 0;
  460. }
  461. </style>