詳解vue-video-player使用心得(兼容m3u8)
更新時間:2019年08月23日 10:39:11 作者:啊阿昕吶
這篇文章主要介紹了詳解vue-video-player使用心得(兼容m3u8),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
下載vue-video-player
npm install vue-video-player --save
在main.js文件引入
import VideoPlayer from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')
Vue.use(VideoPlayer)
在頁面中引入
<video-player
ref="videoPlayer"
class="video-player vjs-custom-skin"
:playsinline="true"
:options="playerOptions"
@play="onPlayerPlay($event)"http://監(jiān)聽開始狀態(tài)
@pause="onPlayerPause($event)"http://監(jiān)聽暫停狀態(tài)
/>
在頁面中data中配置
playerOptions: {
// playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
autoplay: false, // 如果true,瀏覽器準備好時開始回放。
muted: false, // 默認情況下將會消除任何音頻。
loop: false, // 導致視頻一結束就重新開始。
preload: 'auto', // 建議瀏覽器在<video>加載元素后是否應該開始下載視頻數據。auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)
language: 'zh-CN',
aspectRatio: '16:9', // 將播放器置于流暢模式,并在計算播放器的動態(tài)大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數字(例如"16:9"或"4:3")
fluid: true, // 當true時,Video.js player將擁有流體大小。換句話說,它將按比例縮放以適應其容器。
sources: [
{
type: 'video/mp4', // 這里的種類支持很多種:基本視頻格式、直播、流媒體等,具體可以參看git網址項目
src: 'https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm' // url地址
}
],
hls: true,
poster: 'http://pic33.nipic.com/20131007/13639685_123501617185_2.jpg', // 你的封面地址
width: document.documentElement.clientWidth, // 播放器寬度
notSupportedMessage: '此視頻暫無法播放,請稍后再試', // 允許覆蓋Video.js無法播放媒體源時顯示的默認信息。
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true // 全屏按鈕
}
},
掛載視頻組件(非必須)不寫這一步也可以實現播放,添加這個是為了自定義按鈕使用
computed: {
player() {
return this.$refs.videoPlayer.player//自定義播放
}
},
官方文檔
video.js:https://docs.videojs.com/docs/api/player.html
vue-video-player:https://github.com/surmon-china/vue-video-player
不需要兼容m3u8的,以上就可以實現能播放
兼容m3u8的需要下載
npm install --save videojs-contrib-hls
在文件中引入
import ‘videojs-contrib-hls'
我這么引入會出現找不到文件,我沒找到問題所在,如果這么引入不行,可以改為
在main.js文件中
const hls = require('videojs-contrib-hls')
Vue.use(hls)
這樣就好了
在頁面中測試
{
type: 'application/x-mpegURL', // 這里的種類支持很多種:基本視頻格式、直播、流媒體等,具體可以參看git網址項目
src:
'https://cdn.letv-cdn.com/2018/12/05/JOCeEEUuoteFrjCg/playlist.m3u8' // url地址,從別的博主那看來的地址,親測可用
}
至此就可以播放了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

