翼度科技»论坛 编程开发 JavaScript 查看内容

vue2之jessibuca视频插件使用教程详细讲解

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
jessibuca简介
  1. Jessibuca是一款开源的纯H5直播流播放器,通过Emscripten将音视频解码库编译成Js(ams.js/wasm)运行于浏览器之中。兼容几乎所有浏览器,可以运行在PC、手机、微信中,无需额外安装插件。
复制代码
前期准备


下载相关js

从官网下载相关压缩包将文件移入自身项目中,并根据自身vue版本放入Public或者Static文件夹下。
jessibuca官网-源码下载链接  或者 点击这里下载




vue index.html文件引入
  1. <script type="text/javascript" src="/jessibuca/jessibuca.js"></script>
复制代码
组件封装
  1. <template>
  2.   <div
  3.     ref="container"
  4.     @dblclick="fullscreenSwich"
  5.     style="width: 100%; height: 100%; background-color: #000000; margin: 0 auto; position: relative"
  6.   >
  7.     <div
  8.       class="buttons-box"
  9.       id="buttonsBox"
  10.     >
  11.       <div class="buttons-box-left">
  12.         <i
  13.           v-if="!playing"
  14.           class="iconfont icon-play jessibuca-btn"
  15.           @click="playBtnClick"
  16.         ></i>
  17.         <i
  18.           v-if="playing"
  19.           class="iconfont icon-pause jessibuca-btn"
  20.           @click="pause"
  21.         ></i>
  22.         <i
  23.           class="iconfont icon-stop jessibuca-btn"
  24.           @click="destroy"
  25.         ></i>
  26.         <i
  27.           v-if="isNotMute"
  28.           class="iconfont icon-audio-high jessibuca-btn"
  29.           @click="mute()"
  30.         ></i>
  31.         <i
  32.           v-if="!isNotMute"
  33.           class="iconfont icon-audio-mute jessibuca-btn"
  34.           @click="cancelMute()"
  35.         ></i>
  36.       </div>
  37.       <div class="buttons-box-right">
  38.         <span class="jessibuca-btn">{{ kBps }} kb/s</span>
  39.         <i
  40.           class="iconfont icon-camera1196054easyiconnet jessibuca-btn"
  41.           @click="screenshot"
  42.           style="font-size: 1rem !important"
  43.         ></i>
  44.         <i
  45.           class="iconfont icon-shuaxin11 jessibuca-btn"
  46.           @click="playBtnClick"
  47.         ></i>
  48.         <i
  49.           v-if="!fullscreen"
  50.           class="iconfont icon-weibiaoti10 jessibuca-btn"
  51.           @click="fullscreenSwich"
  52.         ></i>
  53.         <i
  54.           v-if="fullscreen"
  55.           class="iconfont icon-weibiaoti11 jessibuca-btn"
  56.           @click="fullscreenSwich"
  57.         ></i>
  58.         <i
  59.           style="font-size: 28px"
  60.           class="iconfont icon-video-close jessibuca-btn"
  61.           @click="closeVideo()"
  62.         ></i>
  63.       </div>
  64.     </div>
  65.   </div>
  66. </template>

  67. <script>
  68. let jessibucaPlayer = {};
  69. export default {
  70.   name: "wkVideoPlayer",
  71.   data() {
  72.     return {
  73.       playing: false,
  74.       isNotMute: false,
  75.       quieting: false,
  76.       fullscreen: false,
  77.       loaded: false, // mute
  78.       speed: 0,
  79.       performance: "", // 工作情况
  80.       kBps: 0,
  81.       btnDom: null,
  82.     };
  83.   },
  84.   props: ["videoUrl", "error", "hasAudio", "height"],
  85.   mounted() {
  86.     this.$nextTick(() => {
  87.       this.updatePlayerDomSize();
  88.       this.btnDom = document.getElementById("buttonsBox");
  89.       window.onresize = () => {
  90.         this.updatePlayerDomSize();
  91.       };
  92.       if (this.videoUrl) {
  93.         this.play(this.videoUrl);
  94.       } else {
  95.         return;
  96.       }
  97.     });
  98.   },
  99.   watch: {
  100.     videoUrl(newData) {
  101.       this.play(newData);
  102.     },
  103.     immediate: true,
  104.   },
  105.   methods: {
  106.     updatePlayerDomSize() {
  107.       let dom = this.$refs.container;
  108.       let width = dom.parentNode.clientWidth;
  109.       let height = dom.parentNode.clientHeight;

  110.       const clientHeight = Math.min(
  111.         document.body.clientHeight,
  112.         document.documentElement.clientHeight
  113.       );
  114.       if (height > clientHeight) {
  115.         height = clientHeight;
  116.         width = (16 / 9) * height;
  117.       }

  118.       dom.style.width = width + "px";
  119.       dom.style.height = height + "px";
  120.     },
  121.     create() {
  122.       let options = {};
  123.       jessibucaPlayer[this._uid] = new window.Jessibuca(
  124.         Object.assign(
  125.           {
  126.             container: this.$refs.container,
  127.             autoWasm: true, // 在使用MSE或者Webcodecs 播放H265的时候,是否自动降级到wasm模式。
  128.             background: "", //背景图片
  129.             controlAutoHide: false, //底部控制台是否自动隐藏
  130.             debug: false, //是否开启控制台调试打印
  131.             decoder: "/jessibuca/decoder.js",
  132.             hasAudio: typeof this.hasAudio == "undefined" ? true : this.hasAudio, // 是否有音频,如果设置false,则不对音频数据解码,提升性能。
  133.             hasVideo: true, //  是否开启控制台调试打印
  134.             heartTimeout: 5, //设置超时时长, 单位秒播放中途,如果超过设定时长无数据返回,则回调timeout事件
  135.             heartTimeoutReplay: true, //是否开启心跳超时之后自动再播放
  136.             heartTimeoutReplayTimes: 3, //重试次数  heartTimeoutReplay 重试失败之后,不再重新播放视频地址。如果想无限次重试,可以设置为-1
  137.             hiddenAutoPause: false, //是否开启当页面的'visibilityState'变为'hidden'的时候,自动暂停播放。
  138.             hotKey: false, //是否开启键盘快捷键 esc -> 退出全屏;arrowUp -> 声音增加;arrowDown -> 声音减少;
  139.             isFlv: false, //当为true的时候:ws协议不检验是否以.flv为依据,进行协议解析。
  140.             isFullResize: false, //当为true的时候:视频画面做等比缩放后,完全填充canvas区域,画面不被拉伸,没有黑边,但画面显示不全
  141.             isNotMute: this.isNotMute, // 是否开启声音,默认是关闭声音播放的。
  142.             isResize: false, //当为true的时候:视频画面做等比缩放后,高或宽对齐canvas区域,画面不被拉伸,但有黑边。 当为false的时候:视频画面完全填充canvas区域,画面会被拉伸。
  143.             keepScreenOn: false, //开启屏幕常亮,在手机浏览器上, canvas标签渲染视频并不会像video标签那样保持屏幕常亮。PC端不会生效,仅手机端生效
  144.             loadingText: "请稍等, 视频加载中......", // 视频加载转圈时的提示文字
  145.             loadingTimeout: 10, //当play()的时候,如果没有数据返回,则回调
  146.             loadingTimeoutReplay: true, ///是否开启loading超时之后自动再播放
  147.             loadingTimeoutReplayTimes: 3, //loadingTimeoutReplay 重试失败之后,不再重新播放视频地址。
  148.             operateBtns: {
  149.               // 配置操作按钮 其中
  150.               fullscreen: false, //全屏按钮
  151.               screenshot: false, //截图按钮
  152.               play: false, //播放暂停按钮
  153.               audio: false, //声音按钮
  154.               record: false, //录制按钮
  155.             },
  156.             recordType: "webm", //默认录制的视频格式
  157.             rotate: 0, //设置旋转角度
  158.             showBandwidth: false, // 显示网速
  159.             supportDblclickFullscreen: false, // 是否支持屏幕的双击事件,触发全屏,取消全屏事件。
  160.             timeout: 10, //设置超时时长, 单位秒在连接成功之前(loading)和播放中途(heart),如果超过设定时长无数据返回,则回调timeout事件
  161.             useMSE: location.hostname !== "localhost" && location.protocol !== "https:", //是否开启MediaSource硬解码
  162.             useWCS: location.hostname === "localhost" || location.protocol === "https", //是否开启Webcodecs硬解码
  163.             useWebFullScreen: false, //是否使用web全屏(旋转90度)(只会在移动端生效)。
  164.             videoBuffer: 0, // 设置最大缓冲时长,单位秒,播放器会自动消除延迟。
  165.             wasmDecodeErrorReplay: true, // 是否开启解码失败重新播放
  166.             wcsUseVideoRender: true, //webcodecs硬解码是否通过video标签渲染
  167.           },
  168.           options
  169.         )
  170.       );
  171.       let jessibuca = jessibucaPlayer[this._uid];
  172.       let _this = this;

  173.       // 监听 jessibuca 初始化事件。
  174.       jessibuca.on("load", function () {
  175.         // console.log("on load init");
  176.       });

  177.       // 信息,包含错误信息
  178.       jessibuca.on("log", function (msg) {
  179.         // console.log("on log", msg);
  180.       });

  181.       // 触发暂停事件
  182.       jessibuca.on("pause", function () {
  183.         _this.playing = false;
  184.       });

  185.       // 触发播放事件
  186.       jessibuca.on("play", function () {
  187.         _this.playing = true;
  188.       });

  189.       // 当前是否全屏
  190.       jessibuca.on("fullscreen", function (msg) {
  191.         // console.log("on fullscreen", msg);
  192.         _this.fullscreen = msg;
  193.       });

  194.       // 触发声音事件,返回boolean值
  195.       jessibuca.on("mute", function (msg) {
  196.         // console.log("on mute", msg);
  197.         _this.isNotMute = !msg;
  198.       });

  199.       // 当解析出音频信息时回调,2个回调参数
  200.       // numOfChannels:声频通道
  201.       // sampleRate 采样率
  202.       // encTypeCode 音频编码类型(10:aac,7:ALAW(g711a),8:MULAW(g711u))
  203.       // encType 音频编码类型(字符串)
  204.       jessibuca.on("audioInfo", function (msg) {
  205.         // console.log("audioInfo", msg);
  206.       });

  207.       let _ts = 0;
  208.       // 当前视频帧pts,单位毫秒ms
  209.       jessibuca.on("timeUpdate", function (ts) {
  210.         _ts = ts;
  211.       });

  212.       // 当解析出视频信息时回调,2个回调参数
  213.       //width:视频宽
  214.       //height:视频高
  215.       //encTypeCode 视频编码类型(10:h264,12:h265)
  216.       //encType 视频编码类型(字符串)
  217.       jessibuca.on("videoInfo", function (info) {
  218.         // console.log("videoInfo", info);
  219.       });

  220.       // 错误信息
  221.       // 目前已有的错误信息:
  222.       // jessibuca.ERROR.playError ;播放错误,url 为空的时候,调用play方法
  223.       // jessibuca.ERROR.fetchError ;http 请求失败
  224.       // jessibuca.ERROR.websocketError; websocket 请求失败
  225.       // jessibuca.ERROR.webcodecsH265NotSupport; webcodecs 解码 h265 失败
  226.       // jessibuca.ERROR.mediaSourceH265NotSupport; mediaSource 解码 h265 失败
  227.       // jessibuca.ERROR.wasmDecodeError ; wasm 解码失败
  228.       jessibuca.on("error", function (error) {
  229.         // console.log("error", error);
  230.       });

  231.       // 当设定的超时时间内无数据返回,则回调
  232.       jessibuca.on("timeout", function () {
  233.         // console.log("timeout");
  234.       });

  235.       // 渲染开始
  236.       jessibuca.on("start", function () {
  237.         // console.log("start");
  238.       });

  239.       // 渲染性能统计,流开始播放后回调,每秒1次。
  240.       // 0: 表示卡顿
  241.       // 1: 表示流畅
  242.       // 2: 表示非常流程
  243.       jessibuca.on("performance", function (performance) {
  244.         let show = "卡顿";
  245.         if (performance === 2) {
  246.           show = "非常流畅";
  247.         } else if (performance === 1) {
  248.           show = "流畅";
  249.         }
  250.         _this.performance = show;
  251.       });

  252.       // 流状态统计,流开始播放后回调,每秒1次。
  253.       // buf: 当前缓冲区时长,单位毫秒,
  254.       // fps: 当前视频帧率,
  255.       // abps: 当前音频码率,单位byte,
  256.       // vbps: 当前视频码率,单位byte,
  257.       // ts:当前视频帧pts,单位毫秒
  258.       jessibuca.on("stats", function (stats) {});

  259.       // 当前网速, 单位KB 每秒1次,
  260.       jessibuca.on("kBps", function (kBps) {
  261.         _this.kBps = Math.round(kBps);
  262.       });

  263.     },
  264.     playBtnClick: function (event) {
  265.       this.play(this.videoUrl);
  266.     },
  267.     play: function (url) {
  268.       console.log(url);
  269.       if (jessibucaPlayer[this._uid]) {
  270.         this.destroy();
  271.       }
  272.       this.create();
  273.       jessibucaPlayer[this._uid].on("play", () => {
  274.         this.playing = true;
  275.         this.loaded = true;
  276.         this.quieting = jessibuca.quieting;
  277.       });
  278.       if (jessibucaPlayer[this._uid].hasLoaded()) {
  279.         jessibucaPlayer[this._uid].play(url);
  280.       } else {
  281.         jessibucaPlayer[this._uid].on("load", () => {
  282.           console.log("load 播放");
  283.           jessibucaPlayer[this._uid].play(url);
  284.         });
  285.       }
  286.     },
  287.     pause: function () {
  288.       if (jessibucaPlayer[this._uid]) {
  289.         jessibucaPlayer[this._uid].pause();
  290.       }
  291.       this.playing = false;
  292.       this.err = "";
  293.       this.performance = "";
  294.     },
  295.     screenshot: function () {
  296.       if (jessibucaPlayer[this._uid]) {
  297.         jessibucaPlayer[this._uid].screenshot();
  298.       }
  299.     },
  300.     mute: function () {
  301.       if (jessibucaPlayer[this._uid]) {
  302.         jessibucaPlayer[this._uid].mute();
  303.       }
  304.     },
  305.     cancelMute: function () {
  306.       if (jessibucaPlayer[this._uid]) {
  307.         jessibucaPlayer[this._uid].cancelMute();
  308.       }
  309.     },
  310.     destroy: function () {
  311.       if (jessibucaPlayer[this._uid]) {
  312.         jessibucaPlayer[this._uid].destroy();
  313.       }
  314.       if (document.getElementById("buttonsBox") == null) {
  315.         this.$refs.container.appendChild(this.btnDom);
  316.       }
  317.       jessibucaPlayer[this._uid] = null;
  318.       this.playing = false;
  319.       this.err = "";
  320.       this.performance = "";
  321.     },
  322.     eventcallbacK: function (type, message) {},
  323.     fullscreenSwich: function () {
  324.       let isFull = this.isFullscreen();
  325.       jessibucaPlayer[this._uid].setFullscreen(!isFull);
  326.       this.fullscreen = !isFull;
  327.     },
  328.     isFullscreen: function () {
  329.       return (
  330.         document.fullscreenElement ||
  331.         document.msFullscreenElement ||
  332.         document.mozFullScreenElement ||
  333.         document.webkitFullscreenElement ||
  334.         false
  335.       );
  336.     },
  337.     closeVideo: function () {
  338.       this.destroy();
  339.       this.$emit("videoClose");
  340.     },
  341.   },
  342. };
  343. </script>

  344. <style>
  345. .buttons-box {
  346.   width: 100%;
  347.   height: 28px;
  348.   background-color: rgba(43, 51, 63, 0.7);
  349.   position: absolute;
  350.   display: -webkit-box;
  351.   display: -ms-flexbox;
  352.   display: flex;
  353.   left: 0;
  354.   bottom: 0;
  355.   user-select: none;
  356.   z-index: 10;
  357. }

  358. .jessibuca-btn {
  359.   width: 20px;
  360.   color: rgb(255, 255, 255);
  361.   line-height: 27px;
  362.   margin: 0px 10px;
  363.   padding: 0px 2px;
  364.   cursor: pointer;
  365.   text-align: center;
  366.   font-size: 0.8rem !important;
  367. }

  368. .buttons-box-right {
  369.   position: absolute;
  370.   right: 0;
  371. }
  372. @font-face {
  373.   font-family: "iconfont"; /* Project id 1291092 */
  374.   src: url("~@/assets/iconfont/iconfont.woff2?t=1673251105600") format("woff2");
  375. }

  376. .iconfont {
  377.   font-family: "iconfont" !important;
  378.   font-size: 16px;
  379.   font-style: normal;
  380.   -webkit-font-smoothing: antialiased;
  381.   -moz-osx-font-smoothing: grayscale;
  382. }

  383. .icon-play:before {
  384.   content: "\e603";
  385. }

  386. .icon-pause:before {
  387.   content: "\e6c6";
  388. }

  389. .icon-stop:before {
  390.   content: "\e6a8";
  391. }

  392. .icon-audio-high:before {
  393.   content: "\e793";
  394. }

  395. .icon-audio-mute:before {
  396.   content: "\e792";
  397. }

  398. .icon-shuaxin11:before {
  399.   content: "\e720";
  400. }

  401. .icon-weibiaoti10:before {
  402.   content: "\e78f";
  403. }

  404. .icon-weibiaoti11:before {
  405.   content: "\e790";
  406. }

  407. .icon-camera1196054easyiconnet:before {
  408.   content: "\e791";
  409. }
  410. </style>
复制代码
使用
  1.      <wk-video-player
  2.        class="video"
  3.        ref="player"
  4.        :videoUrl="videoUrl"
  5.        @videoClose="onVideoClose"
  6.        />
  7. ---         
  8. videoUrl:'直播流url地址'
  9. onVideoClose() {
  10.    this.$set(this.videoUrl, "");
  11. },
复制代码
小知识 引入iconfont

将iconfont 下载好的图标 放入assets即可,该字体已上传


总结

到此这篇关于vue2之jessibuca视频插件使用的文章就介绍到这了,更多相关vue2 jessibuca视频插件使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/326818n73.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具