美文网首页
前端使用video.js 播放视频流

前端使用video.js 播放视频流

作者: 银角大王__ | 来源:发表于2021-11-25 11:28 被阅读0次
    1. 安装video.js
       npm install video.js
    
    1. 引入video.js
    import videojs from "video.js";
    import "video.js/dist/video-js.min.css";
    
    1. 应用
    <template>
         <video  class="video-js"  ref="videoPlayer"  id="videoPlayer"  style="width: 100%; height: 100%" v-if="visible"></video>
    </template>
    
    <script>
    import videojs from "video.js";
    import "video.js/dist/video-js.min.css";
    export default {
      name: "VideoDialog",
      data() {
        return {
          options: {
            autoplay: true,
            controls: true,
            fluid: true,
            aspectRatio: "16:9",
            language: "zh-CN",
            techOrder: ["html5"],
            sourceOrder: true,
            flash: { hls: { withCredentials: false } },
            html5: { hls: { withCredentials: false } },
            sources: [
              {
                withCredentials: false,
                type: "application/x-mpegURL",
                src: "https://cctvalih5ca.v.myalicdn.com/live/cctv1_2/index.m3u8",  
              },
            ],
          }
        };
      },
      mounted(){
          setTimeout(() => {
            this.$nextTick(() => {
              this.initVideo();
            },300);
          });
      },
      methods: {
       initVideo() {
          this.player = videojs(
            this.$refs.videoPlayer,
            this.options,
            function onPlayerReady() {}
          );
        }
      }
    };
    </script>
    
    

    结束

    如需兼容播放mp4之类的视频,动态改下this.options.sources[0].type = '';

     let fileName = this.options.sources[0].src.split('.').pop().toLowerCase();
      if (fileName && fileName ==='mp4'){
              this.options.sources[0].type = '';
       }else{
              this.options.sources[0].type = "application/x-mpegURL";
       }
    

    相关文章

      网友评论

          本文标题:前端使用video.js 播放视频流

          本文链接:https://www.haomeiwen.com/subject/gwuatrtx.html