国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

如何在vue中使用video.js播放m3u8格式的視頻

瀏覽:5日期:2022-10-07 09:14:45

@[toc] 注意:

'vue': '^2.6.11', 'video.js': '^7.10.2', 'videojs-contrib-hls': '^5.15.0', 'mux.js': '^5.7.0'一、安裝

yarn add video.jsyarn add videojs-contrib-hls // 這是播放hls流需要的插件yarn add mux.js // 在vue項目中,若不安裝它可能報錯二、引入videojs

1.在src文件夾下新建 plugins文件夾,并新建video.js文件;

video.js文件的內容如下:

import 'video.js/dist/video-js.css'; // 引入video.js的cssimport hls from 'videojs-contrib-hls'; // 播放hls流需要的插件import Vue from 'vue';Vue.use(hls);

2.在main.js中引入剛剛的video.js文件

import './plugins/video.js'; // 引入剛剛定義的video.js文件三、在組件中測試并使用1. 實現基本的自動播放

Test.vue文件

<template> <div class='test-videojs'> <video muted></video> </div></template><script>import Videojs from 'video.js'; // 引入Videojs export default { data() { return { // https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8 是一段視頻,可將cctv1 (是live現場直播,不能快退)的替換為它,看到快進快退的效果 nowPlayVideoUrl: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8' }; }, mounted() { this.initVideo(this.nowPlayVideoUrl); }, methods: { initVideo(nowPlayVideoUrl) { // 這些options屬性也可直接設置在video標簽上,見 muted let options = { autoplay: true, // 設置自動播放 controls: true, // 顯示播放的控件 sources: [ // 注意,如果是以option方式設置的src,是不能實現 換臺的 (即使監聽了nowPlayVideoUrl也沒實現) { src: nowPlayVideoUrl, type: 'application/x-mpegURL' // 告訴videojs,這是一個hls流 } ] }; // videojs的第一個參數表示的是,文檔中video的id const myPlyer = Videojs('videoPlayer', options, function onPlayerReady() { console.log('onPlayerReady 中的this指的是:', this); // 這里的this是指Player,是由Videojs創建出來的實例 console.log(myPlyer === this); // 這里返回的是true }); } }};</script><style lang='scss'>#videoPlayer { width: 500px; height: 300px; margin: 50px auto;}</style>

視頻播放效果如圖:

如何在vue中使用video.js播放m3u8格式的視頻

打印結果如圖:

如何在vue中使用video.js播放m3u8格式的視頻

2. 實現換臺

Test.vue文件

<template> <div class='test-videojs'> <video class='video-js'></video> <el-button type='danger' @click='changeSource'>切換到CCTV3</el-button> </div></template><script>import Videojs from 'video.js'; // 引入Videojsexport default { data() { return { nowPlayVideoUrl: 'http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8', options: { autoplay: true, // 設置自動播放 muted: true, // 設置了它為true,才可實現自動播放,同時視頻也被靜音 (Chrome66及以上版本,禁止音視頻的自動播放) preload: 'auto', // 預加載 controls: true // 顯示播放的控件 }, player:null }; }, mounted() { this.getVideo(this.nowPlayVideoUrl); }, methods: { getVideo(nowPlayVideoUrl) { this.player = Videojs('videoPlayer', this.options); //關鍵代碼, 動態設置src,才可實現換臺操作 this.player.src([ { src: nowPlayVideoUrl, type: 'application/x-mpegURL' // 告訴videojs,這是一個hls流 } ]); }, changeSource() { this.nowPlayVideoUrl = 'http://ivi.bupt.edu.cn/hls/cctv3hd.m3u8'; console.log(this.nowPlayVideoUrl, '改變了'); } }, watch: { nowPlayVideoUrl(newVal, old) { this.getVideo(newVal); } }, beforeDestroy() { if (this.player) { this.player.dispose(); // Removing Players,該方法會重置videojs的內部狀態并移除dom } }};</script><style lang='scss'>#videoPlayer { width: 500px; height: 300px; margin: 50px auto;}</style>

視頻切換效果如圖:

如何在vue中使用video.js播放m3u8格式的視頻

如何在vue中使用video.js播放m3u8格式的視頻

四、踩坑小記1. 視頻不能自動播放 或報錯 DOMException: play() failed

需用muted屬性解決

報錯信息:DOMException: play() failedbecause the user didn’t interact with the document first.(用戶還沒有交互,不能調用play)

解決辦法:設置muted屬性為true

<video muted></video>

關于muted屬性:

muted 屬性,設置或返回音頻是否應該被靜音(關閉聲音);屬性的值是true和false; muted='false' 表示視頻不用靜音(視頻播放便有聲音),但設置 muted='fasle' 的情況下,視頻無法實現自動播放。 video 標簽中 muted 的作用: 允許視頻自動播放;(Chrome66版本開始,禁止視頻和音頻的自動播放) 2. 換臺的時候,url已經成功更改,但視頻還是之前的

需得動態設置src才能實現

// 動態設置srcthis.player.src([ { src: nowPlayVideoUrl, type: 'application/x-mpegURL' // 告訴videojs,這是一個hls流 } ]);3. 找不到mux.js模塊

報錯信息:* mux.js/lib/tools/parse-sidx in ./node_modules/video.js/dist/video.es.js To install it, you can run: npm install --save mux.js/lib/tools/parse-sidx

解決辦法:安裝mux.js

yarn add mux.js五、 播放rtmp流

播放rtmp流的操作與播放hls流的操作幾乎相同,不同在于:

import 'videojs-flash'; // 播放rtmp流需要的插件type: ’rtmp/flv’, // 這個type值必寫, 告訴videojs這是一個rtmp流視頻

以上就是如何在vue中使用video.js 播放m3u8格式的視頻的詳細內容,更多關于vue 使用videojs 播放m3u8格式的視頻的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 钦州市| 石阡县| 衡阳市| 罗源县| 阿合奇县| 乡城县| 历史| 壶关县| 富川| 迁西县| 昭觉县| 方正县| 聂拉木县| 紫云| 桂阳县| 贺州市| 独山县| 澜沧| 无锡市| 北安市| 靖远县| 禹州市| 綦江县| 江川县| 潼南县| 电白县| 剑河县| 稻城县| 招远市| 桐庐县| 迭部县| 汉寿县| 白沙| 微山县| 和田市| 长沙县| 福泉市| 江油市| 资中县| 石柱| 陆良县|