下載vue-video-playercss
npm install vue-video-player --save
複製代碼
在main.js文件引入html
import VideoPlayer from 'vue-video-player'
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')
Vue.use(VideoPlayer)
複製代碼
在頁面中引入vue
<video-player
ref="videoPlayer"
class="video-player vjs-custom-skin"
:playsinline="true"
:options="playerOptions"
@play="onPlayerPlay($event)"//監聽開始狀態
@pause="onPlayerPause($event)"//監聽暫停狀態
/>
複製代碼
在頁面中data中配置git
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', // 將播放器置於流暢模式,並在計算播放器的動態大小時使用該值。值應該表明一個比例 - 用冒號分隔的兩個數字(例如"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 // 全屏按鈕
}
},
複製代碼
掛載視頻組件(非必須)不寫這一步也能夠實現播放,添加這個是爲了自定義按鈕使用github
computed: {
player() {
return this.$refs.videoPlayer.player//自定義播放
}
},
複製代碼
官方文檔web
video.js:docs.videojs.com/docs/api/pl… vue-video-player:github.com/surmon-chin…npm
不須要兼容m3u8的,以上就能夠實現能播放 兼容m3u8的須要下載api
npm install --save videojs-contrib-hls
複製代碼
在文件中引入瀏覽器
import ‘videojs-contrib-hls’
我這麼引入會出現找不到文件,我沒找到問題所在,若是這麼引入不行,能夠改成
在main.js文件中
const hls = require('videojs-contrib-hls')
Vue.use(hls)
這樣就行了
複製代碼
在頁面中測試bash
{
type: 'application/x-mpegURL', // 這裏的種類支持不少種:基本視頻格式、直播、流媒體等,具體能夠參看git網址項目
src:
'https://cdn.letv-cdn.com/2018/12/05/JOCeEEUuoteFrjCg/playlist.m3u8' // url地址,從別的博主那看來的地址,親測可用
}
複製代碼
至此就能夠播放了