vue3使用video.js播放m3u8格式视频

有时候我们需要播放 m3u8 格式的视频,或者实现更多定制化需求,HTML 的 video 元素无法满足所需,这时候可以考虑使用 Video.js。

video.js是为HTML5世界从零开始构建的网络视频播放器。它支持HTML5视频和现代流媒体格式,以及YouTube和Vimeo。

videojs 官网:https://videojs.com/

videojs 中文文档:https://gitcode.gitcode.host/docs-cn/video.js-docs-cn/index.html

实现一个VideoJs播放器组件

vue3使用video.js播放m3u8格式视频

视频封面图片来自unsplash

安装依赖

npm i video.js

M3U8 是一种基于 HTTP Live Streaming (HLS) 技术的媒体播放列表格式,所以我们还需要安装依赖。

npm i videojs-contrib-hls

播放器组件代码

<template>
  <div v-bind="$attrs" class="videoPlayer">
    <video
      class="video-js"
      :id="id"
      style="width: 100%; height: 100%"
      :poster="poster"
    >

      <source v-if="src" :src="src" />
    </video>
  </div>

</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount, watch, ref } from 'vue'
import 'video.js/
dist/video-js.css'
import videojs from '
video.js'

const overrideNative = ref(false)
const props = defineProps({
  id: { type: String, default: '
vd' },
  src: { type: String, default: '
' },
  poster: { type: String, default: '
' }
})

const emit = defineEmits([
  '
videoCanplaythrough',
  '
videoPlay',
  '
videoPlaying',
  '
videoPause'
])

// VideoJs更多选项配置可以参考中文文档:
// https://gitcode.gitcode.host/docs-cn/video.js-docs-cn/docs/guides/options.html
function options() {
  return {
    html5: {
      hls: {
        overrideNative: overrideNative
      },
      nativeVideoTracks: !overrideNative,
      nativeAudioTracks: !overrideNative,
      nativeTextTracks: !overrideNative
    },
    autoplay: true, // true,浏览器准备好时开始播放。
    muted: false, // 默认情况下将会消除音频。
    loop: false, // 导致视频一结束就重新开始。
    controls: true,
    preload: '
auto', // auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
    fluid: true, // 当true时,将按比例缩放以适应其容器。
    type: '
application/x-mpegURl',
    notSupportedMessage: '
此视频暂无法播放,请稍后再试', // 无法播放媒体源时显示的默认信息。
    textTrackDisplay: false
  }
}

let player: any

onMounted(() => {
  try {
    player = videojs(props.id, options(), () => {
      videojs.log('
播放器已经准备好了!')
      player.pause()
      player.on('
canplaythrough', function (event: any) {
        emit('
videoCanplaythrough', event.target.player.cache_?.duration)
      })
      player.on('
play', function () {
        videojs.log('
视频准备播放')
        emit('
videoPlay')
      })
      player.on('
playing', function () {
        videojs.log('
视频已开始播放')
        emit('
videoPlaying')
      })
      player.on('
pause', function (event: any) {
        emit('
videoPause', event.target.player.cache_?.currentTime)
      })
    })
  } catch (error) {
    console.log('
catch--->', error)
  }
})

onBeforeUnmount(() => {
  if (player) {
    player.dispose()
  }
})
</script>

<style scoped>
.videoPlayer {
  width: 100%;
  max-width: 640px;
  height: 360px;
  position: relative;
  overflow: hidden;
}

.video-js {
  padding-top: 0 !important;
}
</style>

组件使用

<template>
  <div>  
      <VideoPlayer
        :src="src"
        id="videoPlayer"
        :poster="require('~/assets/images/photo-1622208489373-1fe93e2c6720.avif')"
      />   
  </div>
</template>

<script setup>
import { ref } from 'vue'

const src = ref(
  'https://xxx.m3u8' 
)


</script>

设置语言为中文

基础的播放器已经写好了,但是现在播放器自带的语言还是英文,我们需要设置为中文。video.js 自带了很多语言包,按需设置即可。

import video_zhCN from 'video.js/dist/lang/zh-CN.json'
videojs.addLanguage('zh-CN', video_zhCN)

// ...
function options({
  return {
    // ...
    language'zh-CN'
  }
}

OK,VideoJs 播放器的文字已经变成中文了。

vue3使用video.js播放m3u8格式视频

image.png

如何同时使用多个VideoJs播放器组件

在页面中直接复制出一个视频组件,发现只有第一个可以正常播放,第二个播放器不能使用。

vue3使用video.js播放m3u8格式视频


这个问题也很好解决,上面的组件已经通过props提供了id属性,只需给两个组件设置不同的id即可。

 <VideoPlayer
        :src="src"
        id="vd1"
        :poster="require('~/assets/images/photo-1622208489373-1fe93e2c6720.avif')"
      />

你可能还会遇到切换视频没有变化的的问题,通过为每次播放的视频设置独一无二的id即可解决,类似:id="uuid()"

指定播放时间点

通过设置currentTime,可以让视频从某个时间点开始播放

watch(
  () => props.currentTime,
  () => {
    player.currentTime(Number(props.currentTime))
  }
)

onMounted(() => {
  try {
    player = videojs(props.id, options(), () => {
      // ...
      player.on('timeupdate'function (event: any{
        emit('videoTimeupdate', event.target.player.cache_?.currentTime)
      })
      // ...
    })
  }
})


欢迎交流技术问题~

原文始发于微信公众号(自由前端之路):vue3使用video.js播放m3u8格式视频

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/295102.html

(0)
码上实战的头像码上实战

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!