關(guān)于vue播放flv,m3u8視頻流(監(jiān)控)的方法

2023-4-19    前端達(dá)人

前文:隨著前端大屏頁面的逐漸壯大,客戶的需求也越來越多,大屏上面展示的事物也越來越豐     富。其中實時播放監(jiān)控的需求逐步增加,視頻流格式也是有很多種,用到最多的.flv、.m3u8。

一、 JessibucaPlayer插件用來播放flv流

1.首先是先把文件放在vue項目的public下面

2.在index.html文件里面引入 index.js文件(直接引入即可)

 3.把封裝好的JessibucaPlayer組件放到公共components


  1. <template>
  2. <div class="jessibuca-player" style="width: 100%; height: 100%">
  3. <div class="container" :id="id" ref="container"></div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "JessibucaPlayer",
  9. props: {
  10. videoUrl: {
  11. type: String,
  12. default: ""
  13. },
  14. id: {
  15. type: Number,
  16. required: true
  17. }
  18. },
  19. data() {
  20. return {
  21. jessibuca: null // jessibuca 實例化對象
  22. };
  23. },
  24. methods: {
  25. init() {
  26. this.jessibuca = new window.Jessibuca({
  27. container: document.getElementById(this.id), // 播放器容器,若為 string ,則底層調(diào)用的是 document.getElementById('id')
  28. videoBuffer: 0.2, // 設(shè)置最大緩沖時長,單位毫秒,播放器會自動消除延遲。
  29. forceNoOffscreen: true, // 是否不使用離屏模式(提升渲染能力)
  30. hasAudio: false, // 是否有音頻
  31. rotate: 0, // 設(shè)置旋轉(zhuǎn)角度,只支持,0(默認(rèn)) ,180,270 三個值
  32. isResize: false, // 1.當(dāng)為true的時候:視頻畫面做等比縮放后,高或?qū)拰Rcanvas區(qū)域,畫面不被拉伸,但有黑邊;2.當(dāng)為false的時候:視頻畫面完全填充canvas區(qū)域,畫面會被拉伸
  33. isFullSize: true, // 當(dāng)為true的時候:視頻畫面做等比縮放后,完全填充canvas區(qū)域,畫面不被拉伸,沒有黑邊,但畫面顯示不全
  34. debug: false, // 是否開啟控制臺調(diào)試打印
  35. timeout: 30, // 設(shè)置超時時長, 單位秒,在連接成功之前和播放中途,如果超過設(shè)定時長無數(shù)據(jù)返回,則回調(diào)timeout事件
  36. supportDblclickFullscreen: true, // 是否支持屏幕的雙擊事件,觸發(fā)全屏,取消全屏事件。
  37. showBandwidth: false, // 是否顯示網(wǎng)速
  38. operateBtns: {
  39. // 配置功能
  40. fullscreen: false, // 是否顯示全屏按鈕
  41. screenshot: false, // 是否顯示截圖按鈕
  42. play: false, // 是否顯示播放暫停按鈕
  43. audio: false // 是否顯示聲音按鈕
  44. },
  45. keepScreenOn: false, // 開啟屏幕常亮,在手機(jī)瀏覽器上, canvas標(biāo)簽渲染視頻并不會像video標(biāo)簽?zāi)菢颖3制聊怀A痢?/span>
  46. isNotMute: false, // 是否開啟聲音,默認(rèn)是關(guān)閉聲音播放的。
  47. loadingText: "加載中...", // 加載過程中文案。
  48. background: "" // 背景圖片。
  49. });
  50. // 事件:
  51. this.jessibuca_load()
  52. // 1. 監(jiān)聽 jessibuca 初始化事件。
  53. this.jessibuca.on("load", () => {
  54. this.jessibuca_load()});
  55. // 2. 信息,包含錯誤信息
  56. this.jessibuca.on("log", data => this.jessibuca_log(data));
  57. // 3. 觸發(fā)暫停事件
  58. this.jessibuca.on("pause", this.jessibuca_pause);
  59. // 4. 觸發(fā)播放事件
  60. this.jessibuca.on("play", this.jessibuca_play);
  61. // 5. 當(dāng)前是否全屏
  62. this.jessibuca.on("fullscreen", this.jessibuca_fullscreen);
  63. // 6. 觸發(fā)聲音事件,返回boolean值
  64. this.jessibuca.on("mute", this.jessibuca_mute);
  65. // 7. 當(dāng)解析出音頻信息時回調(diào)
  66. this.jessibuca.on("audioInfo", this.jessibuca_audioInfo);
  67. // 8. 當(dāng)解析出視頻信息時回調(diào)
  68. this.jessibuca.on("videoInfo", this.jessibuca_videoInfo);
  69. // 9. 錯誤信息
  70. this.jessibuca.on("error", this.jessibuca_error);
  71. // 10. 當(dāng)設(shè)定的超時時間內(nèi)無數(shù)據(jù)返回,則回調(diào)
  72. this.jessibuca.on("timeout", this.jessibuca_timeout);
  73. // 11. 流狀態(tài)統(tǒng)計,流開始播放后回調(diào),每秒1次
  74. this.jessibuca.on("start", this.jessibuca_start);
  75. // 12. 渲染性能統(tǒng)計,流開始播放后回調(diào),每秒1次
  76. this.jessibuca.on("performance", this.jessibuca_performance);
  77. // 13. 當(dāng)前網(wǎng)速, 單位KB 每秒1次,
  78. this.jessibuca.on("kBps", this.jessibuca_kBps);
  79. },
  80. // 事件:
  81. jessibuca_load() {
  82. // 監(jiān)聽 jessibuca 初始化事件。
  83. console.log("on load");
  84. console.log("module", this.videoUrl);
  85. this.methods_play(this.videoUrl);
  86. },
  87. jessibuca_log(data) {
  88. // 信息,包含錯誤信息
  89. console.log("on log", data);
  90. },
  91. jessibuca_pause(flag) {
  92. // 觸發(fā)暫停事件
  93. console.log("on pause", flag);
  94. },
  95. jessibuca_play(flag) {
  96. // 觸發(fā)播放事件
  97. console.log("on play", flag);
  98. },
  99. jessibuca_fullscreen(flag) {
  100. // 當(dāng)前是否全屏
  101. console.log("on fullscreen", flag);
  102. if (flag) {
  103. let myEvent = new Event("resize");
  104. setTimeout(() => {
  105. window.dispatchEvent(myEvent);
  106. }, 100);
  107. }
  108. },
  109. jessibuca_mute(flag) {
  110. // 觸發(fā)聲音事件
  111. console.log("on mute", flag);
  112. },
  113. jessibuca_audioInfo(data) {
  114. // 當(dāng)解析出音頻信息時回調(diào),2個回調(diào)參數(shù)
  115. // 1. numOfChannels:聲頻通道
  116. // 2. sampleRate 采樣率
  117. console.log("audioInfo", data);
  118. },
  119. jessibuca_videoInfo(data) {
  120. // 當(dāng)解析出視頻信息時回調(diào)
  121. // 1. w:視頻寬
  122. // 2. h:視頻高
  123. console.log("videoInfo", data);
  124. },
  125. jessibuca_error(error) {
  126. // 錯誤信息
  127. console.log("error:", error);
  128. },
  129. jessibuca_timeout() {
  130. // 當(dāng)設(shè)定的超時時間內(nèi)無數(shù)據(jù)返回,則回調(diào)
  131. console.log("timeout");
  132. },
  133. jessibuca_start(s) {
  134. // 流狀態(tài)統(tǒng)計,流開始播放后回調(diào),每秒1次
  135. // 1. buf: 當(dāng)前緩沖區(qū)時長,單位毫秒
  136. // 2. fps: 當(dāng)前視頻幀率,
  137. // 3. abps: 當(dāng)前音頻碼率,單位bit,
  138. // 4. vbps: 當(dāng)前視頻碼率,單位bit,
  139. // 5. ts:當(dāng)前視頻幀pts,單位毫秒
  140. // console.log('stats is', s);
  141. },
  142. jessibuca_performance(performance) {
  143. // 渲染性能統(tǒng)計,流開始播放后回調(diào),每秒1次。
  144. // 0: 表示卡頓、1: 表示流暢、2: 表示非常流程
  145. // console.log('performance', performance);
  146. },
  147. jessibuca_kBps(kBps) {
  148. // 當(dāng)前網(wǎng)速, 單位KB 每秒1次,
  149. // console.log('kBps', kBps);
  150. },
  151. // 方法:
  152. methods_play(url) {
  153. // 播放
  154. if (this.jessibuca.hasLoaded()) {
  155. this.jessibuca.play(url);
  156. } else {
  157. console.error("視頻數(shù)據(jù)未加載完畢,請稍后操作");
  158. }
  159. },
  160. methods_mute() {
  161. // 靜音
  162. this.jessibuca.mute();
  163. },
  164. methods_cancelMute() {
  165. // 取消靜音
  166. this.jessibuca.cancelMute();
  167. },
  168. methods_pause() {
  169. // 暫停
  170. this.jessibuca.pause();
  171. },
  172. methods_setVolume(volume) {
  173. // 設(shè)置音量
  174. this.jessibuca.setVolume(volume);
  175. },
  176. methods_setRotate(rotate) {
  177. // 設(shè)置旋轉(zhuǎn)角度 0\180\270
  178. this.jessibuca.setRotate(rotate);
  179. },
  180. methods_destroy() {
  181. // 關(guān)閉視頻,釋放底層資源
  182. if (this.jessibuca) {
  183. this.jessibuca.destroy();
  184. }
  185. },
  186. methods_fullscreen(flag) {
  187. // 全屏(取消全屏)播放視頻
  188. this.jessibuca.setFullscreen(flag);
  189. },
  190. methods_screenShot() {
  191. // 截圖
  192. // 1. screenshot(filename, format, quality)
  193. // 2. {string} filename、 {string} format、{number} quality
  194. // 3.filename: 保存的文件名, 默認(rèn) 時間戳、format : 截圖的格式,可選png或jpeg或者webp ,默認(rèn) png、quality: 可選參數(shù),當(dāng)格式是jpeg或者webp時,壓縮質(zhì)量,取值0 ~ 1 ,默認(rèn) 0.92
  195. this.jessibuca.screenshot();
  196. },
  197. methods_setBufferTime(time) {
  198. // 設(shè)置最大緩沖時長,單位秒,播放器會自動消除延遲。
  199. // {number} time
  200. this.jessibuca.setBufferTime(Number(time));
  201. },
  202. methods_setScaleMode(mode) {
  203. // 設(shè)置播放器填充
  204. // 1. 0 視頻畫面完全填充canvas區(qū)域,畫面會被拉伸 等同于參數(shù) isResize 為false
  205. // 2. 1 視頻畫面做等比縮放后,高或?qū)拰Rcanvas區(qū)域,畫面不被拉伸,但有黑邊 等同于參數(shù) isResize 為true
  206. // 3. 2 視頻畫面做等比縮放后,完全填充canvas區(qū)域,畫面不被拉伸,沒有黑邊,但畫面顯示不全 等同于參數(shù) isFullSize 為true
  207. this.jessibuca.setScaleMode(Number(mode));
  208. },
  209. restartPlay() {
  210. // 重新加載
  211. this.methods_destroy();
  212. this.methods_play(this.videoUrl);
  213. }
  214. },
  215. mounted() {
  216. this.init();
  217. window.onerror = msg => (this.err = msg);
  218. },
  219. beforeDestroy() {
  220. if (this.jessibuca) this.jessibuca.destroy();
  221. }
  222. };
  223. </script>
  224. <style>
  225. @import url("./JessibucaPlayer.css");
  226. </style>

 4.最后再自己用到的文件里面 引入組件即可

 如有想要文件的請私聊

二、easyplayer插件播放m3u8流

教程:

1.首先npm安裝EasyPlayer、copy-webpack-plugin
ps:copy-webpack-plugin版本一定一定一定不能大于6.0,否則會報錯。


  1. npm install @easydarwin/easyplayer --save
  2. npm install copy-webpack-plugin@5.1.2 --save-dev

2.最重要的地方 一定要把這個地方弄好 那就是在vue.config.js里面


  1. const CopyWebpackPlugin = require('copy-webpack-plugin')
  2. configureWebpack: {
  3. plugins:[
  4. new CopyWebpackPlugin([
  5. {
  6. from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer.swf',
  7. to: './libs/EasyPlayer/'
  8. },
  9. {
  10. from: 'node_modules/@easydarwin/easyplayer/dist/component/crossdomain.xml',
  11. to: './libs/EasyPlayer/'
  12. },
  13. {
  14. from: 'node_modules/@easydarwin/easyplayer/dist/component/EasyPlayer-lib.min.js',
  15. to: './libs/EasyPlayer/'
  16. }
  17. ])
  18. ]
  19. }

3.還需要在public/index.html 引入EasyPlayer-lib.min.js,可以直接在node_modules里找到@easydarwin下的dist/compent/EasyPlayer-lib.min.js復(fù)制到pubilc目錄下,還有需要EasyPlayer.wasm同樣放到public目錄下

 4.引入組件使用

 最后效果



藍(lán)藍(lán)設(shè)計建立了UI設(shè)計分享群,每天會分享國內(nèi)外的一些優(yōu)秀設(shè)計,如果有興趣的話,可以進(jìn)入一起成長學(xué)習(xí),請加微信ban_lanlan,報下信息,藍(lán)小助會請您入群。歡迎您加入噢~~
希望得到建議咨詢、商務(wù)合作,也請與我們聯(lián)系01063334945。 

分享此文一切功德,皆悉回向給文章原作者及眾讀者. 免責(zé)聲明:藍(lán)藍(lán)設(shè)計尊重原作者,文章的版權(quán)歸原作者。如涉及版權(quán)問題,請及時與我們?nèi)〉寐?lián)系,我們立即更正或刪除。 

藍(lán)藍(lán)設(shè)計www.bouu.cn )是一家專注而深入的界面設(shè)計公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計、BS界面設(shè)計 、 cs界面設(shè)計 、 ipad界面設(shè)計 、 包裝設(shè)計 、 圖標(biāo)定制 、 用戶體驗 、交互設(shè)計、 網(wǎng)站建設(shè) 、平面設(shè)計服務(wù)、UI設(shè)計公司、界面設(shè)計公司、UI設(shè)計服務(wù)公司、數(shù)據(jù)可視化設(shè)計公司、UI交互設(shè)計公司、高端網(wǎng)站設(shè)計公司、UI咨詢、用戶體驗公司、軟件界面設(shè)計公司

分享本文至:

日歷

鏈接

個人資料

存檔