vue播放video插件vue-video-player實現hls, rtmp播放全過程


1.安裝插件

1 npm install vue-video-player -S

2.配置插件 
在main.js里添加

1 import VideoPlayer from 'vue-video-player'
2 require('video.js/dist/video-js.css')
3 require('vue-video-player/src/custom-theme.css')
4 Vue.use(VideoPlayer)

3.使用插件 
在vue的組件頁面里

復制代碼
 1 <template>
 2         <video-player  class="video-player vjs-custom-skin"
 3             ref="videoPlayer"
 4             :playsinline="true"
 5             :options="playerOptions"
 6         ></video-player>
 7 </template>
 8 <script>
 9 export default {
10   name: 'BusImg',
11   data () {
12     return {
13       // 視頻播放
14       playerOptions : {
15         playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
16         autoplay: false, //如果true,瀏覽器准備好時開始回放。
17         muted: false, // 默認情況下將會消除任何音頻。
18         loop: false, // 導致視頻一結束就重新開始。
19         preload: 'auto', // 建議瀏覽器在<video>加載元素后是否應該開始下載視頻數據。auto瀏覽器選擇最佳行為,立即開始加載視頻(如果瀏覽器支持)
20         language: 'zh-CN',
21         aspectRatio: '16:9', // 將播放器置於流暢模式,並在計算播放器的動態大小時使用該值。值應該代表一個比例 - 用冒號分隔的兩個數字(例如"16:9"或"4:3")
22         techOrder: ['flash', 'html5'],      // 兼容順序

23     flash: {
24           hls: { withCredentials: false },
25           swf: './static/media/video-js.swf'         // 引入靜態文件swf
26         },
27         html5: { hls: { withCredentials: false } },
28         sources: [{ // 流配置,數組形式,會根據兼容順序自動切換
29           type: 'rtmp/hls',
30           src: 'rtmp://192.168.1.199:10935/hls/stream_1'
31         }, {
32           withCredentials: false,
33           type: 'application/x-mpegURL',
34           src: 'http://playertest.longtailvideo.com/adaptive/bipbop/gear4/prog_index.m3u8'
35         }],
36         poster: "", //你的封面地址
37         // width: document.documentElement.clientWidth,
38         notSupportedMessage: '此視頻暫無法播放,請稍后再試', //允許覆蓋Video.js無法播放媒體源時顯示的默認信息。
39         controlBar: {
40           timeDivider: true,
41           durationDisplay: true,
42           remainingTimeDisplay: false,
43           fullscreenToggle: true  //全屏按鈕
44         }
45     }
46 </script>
復制代碼

注意點

下面說說用這個插件來直播的一些坑和注意點吧:

首先,常用的 demo 在 vue-video-player 中官方已經給出了,按要求來就可以,其中

  1. 如果需要播放 HLS 流,需要安裝 videojs-contrib-hls 插件,非原生支持的瀏覽器,直播服務端需要開啟 CORS
  2. 如果需要播放 RTMP 流,需要安裝 videojs-flash 插件
  3. 如果兩個流都需要播放,flash 插件需要安裝到 hls 插件之前
  4. 安裝vue-video-player插件一定要用npm安裝,不可使用cnpm安裝,否則會報“The "flash" tech is undefined. Skipped browser support check for that tech”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM