• vue video播放m3u8源


    1,采坑记录


    表现:使用videojs-contrib-hls在develop环境可以正常播放,但在product环境无法播放。
    原因:videojs-contrib-hls组件打包时有无法解析的语法。
    解决:不需要videojs-contrib-hls和vue-video-player,仅需要video.js == 7.20.2


    表现:CCTV源无法播放,如:
            CCTV-1高清 http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8
            CCTV-3高清 http://ivi.bupt.edu.cn/hls/cctv3hd.m3u8
            CCTV-5高清 http://ivi.bupt.edu.cn/hls/cctv5hd.m3u8
            CCTV-5+高清 http://ivi.bupt.edu.cn/hls/cctv5phd.m3u8
            CCTV-6高清 http://ivi.bupt.edu.cn/hls/cctv6hd.m3u8
            CCTV-8高清 http://ivi.bupt.edu.cn/hls/cctv8hd.m3u8
            CHC高清电影 http://ivi.bupt.edu.cn/hls/chchd.m3u8
            北京卫视高清 http://ivi.bupt.edu.cn/hls/btv1hd.m3u8
            北京文艺高清 http://ivi.bupt.edu.cn/hls/btv2hd.m3u8
            北京体育高清 http://ivi.bupt.edu.cn/hls/btv6hd.m3u8
            北京纪实高清 http://ivi.bupt.edu.cn/hls/btv11hd.m3u8
            湖南卫视高清 http://ivi.bupt.edu.cn/hls/hunanhd.m3u8
            浙江卫视高清 http://ivi.bupt.edu.cn/hls/zjhd.m3u8
            江苏卫视高清 http://ivi.bupt.edu.cn/hls/jshd.m3u8
            东方卫视高清 http://ivi.bupt.edu.cn/hls/dfhd.m3u8
            安徽卫视高清 http://ivi.bupt.edu.cn/hls/ahhd.m3u8
            黑龙江卫视高清 http://ivi.bupt.edu.cn/hls/hljhd.m3u8
            辽宁卫视高清 http://ivi.bupt.edu.cn/hls/lnhd.m3u8
            深圳卫视高清 http://ivi.bupt.edu.cn/hls/szhd.m3u8
            广东卫视高清 http://ivi.bupt.edu.cn/hls/gdhd.m3u8
            天津卫视高清 http://ivi.bupt.edu.cn/hls/tjhd.m3u8
            湖北卫视高清 http://ivi.bupt.edu.cn/hls/hbhd.m3u8
            山东卫视高清 http://ivi.bupt.edu.cn/hls/sdhd.m3u8

    原因:未知,可能是必须要https;

    解决:可以播放的源如:
            https://live.cgtn.com/1000/prog_index.m3u8
            http://tinywan.com/live/4001490605096.m3u8
            https://livefr.cgtn.com/1000f/prog_index.m3u8
            https://liveru.cgtn.com/1000r/prog_index.m3u8
            https://livedoc.cgtn.com/1000d/prog_index.m3u8
            https://livear.cgtn.com/1000a/prog_index.m3u8 


     表现:无法切换视频源 VIDEOJS: WARN: Player "[object HTMLDivElement]" is already initialised. Options will not be applied.
     原因:多次调用videojs 需要先卸载组件,再重新加载;
     解决:
     

    1.         if(this.player)
    2.         {
    3.           // 直接换源不起作用,reset也不行,必须dispose(坑1)
    4.           this.player.dispose();
    5.           // var newVideo = document.createElement('video');
    6.           // newVideo.setAttribute('ref', 'videoPlayer'); 
    7.           // newVideo.setAttribute('class', 'video-js');
    8.           // var Fid = document.getElementById('fid');
    9.           // 这种方式会遗留原先的video节点,dispose()没有清理干净(坑2)
    10.           // Fid.appendChild(newVideo);
    11.           document.getElementById('fid').innerHTML = '';
    12.         }
    13.         
    14.         this.$nextTick(() => {
    15.           // 这种方式取不到动态添加的节点(坑3)
    16.           // this.player = videojs(this.$refs.videoPlayer, this.playerOptions);
    17.           this.player = videojs(document.getElementById('videoPlayer'), this.playerOptions);
    18.           this.player.play();
    19.         });

    2,安装和引入依赖

    npm install video.js
    1. import videojs from 'video.js';
    2. import 'video.js/dist/video-js.css';

     

    3,页面

    1. <el-dialog title="实时监控" :visible.sync="showVideo" width="80%" append-to-body :before-close="handleClose">
    2. <div id='fid'>
    3. <video id="videoPlayer" ref="videoPlayer" class="video-js" >video>
    4. div>
    5. el-dialog>

    4,JS

    1. data() {
    2. return {
    3. ...
    4. player: null,
    5. playerOptions:{
    6. autoplay: true, // 如果true,浏览器准备好时开始播放。
    7. muted: false, // 默认情况下将会消除任何音频。
    8. loop: true, // 导致视频一结束就重新开始。
    9. preload: 'auto', // auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
    10. language: 'zh-CN', //汉化
    11. fluid: true, // 当true时,播放器将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
    12. sources: [{
    13. type: 'application/x-mpegURL',
    14. src: 'https://live.cgtn.com/1000/prog_index.m3u8',
    15. }],
    16. notSupportedMessage: '此视频暂无法播放,请稍后再试', // 无法播放媒体源时显示的默认信息。
    17. controlBar: {
    18. timeDivider: true,
    19. durationDisplay: true, //视频时长时间显示
    20. remainingTimeDisplay: false, //剩余时间显示
    21. fullscreenToggle: true // 全屏按钮
    22. },
    23. errorDisplay: false, //错误显示
    24. posterImage: false, //视频的预览海报图片显示
    25. bigPlayButton: true, //视频中间的播放按钮显示
    26. textTrackDisplay: false
    27. },
    28. ...
    29. }
    30. },
    31. methods: {
    32. handleShowVideo(row) {
    33. const ciId = row.ciId || this.id;
    34. getInfo(ciId).then(response => {
    35. const element = response.data;
    36. // 网络摄像头
    37. this.showVideo = true;
    38. this.playerOptions.sources = [
    39. {
    40. type: 'application/x-mpegURL',
    41. src: element.ciUrl
    42. },
    43. ];
    44. if(this.player)
    45. {
    46. // 直接换源不起作用,reset也不行,必须dispose(坑1)
    47. this.player.dispose();
    48. // var newVideo = document.createElement('video');
    49. // newVideo.setAttribute('ref', 'videoPlayer');
    50. // newVideo.setAttribute('class', 'video-js');
    51. // var Fid = document.getElementById('fid');
    52. // 这种方式会遗留原先的video节点,dispose()没有清理干净(坑2)
    53. // Fid.appendChild(newVideo);
    54. document.getElementById('fid').innerHTML = '';
    55. }
    56. this.$nextTick(() => {
    57. // 这种方式取不到动态添加的节点(坑3)
    58. // this.player = videojs(this.$refs.videoPlayer, this.playerOptions);
    59. this.player = videojs(document.getElementById('videoPlayer'), this.playerOptions);
    60. this.player.play();
    61. });
    62. });
    63. // 本地摄像头
    64. // this.initVideo();
    65. },
    66. /** 关闭监控直播对话框 */
    67. handleClose() {
    68. this.showVideo = false;
    69. // 销毁本地摄像头
    70. if (this.video && this.video !== null) {
    71. this.video.srcObject.getTracks().forEach(element => {
    72. element.stop();
    73. });
    74. }
    75. // 销毁网络摄像头
    76. },
    77. }

     

  • 相关阅读:
    【数据结构】堆综合的运用
    09_Webpack打包工具
    Flink-CDC 抽取SQLServer问题总结
    uniapp vue项目把图片路径从data数据传到css(uniapp css变量)
    基于51单片机汽车智能灯光控制系统设计
    HTML5+CSS3小实例:纯CSS实现彩虹倒映水面的唯美背景
    Web 3.0与去中心化应用:未来的互联网是什么样子?
    AI技术:分享8个非常实用的AI绘画网站
    《乔布斯传》英文原著重点词汇笔记(十五)【 chapter fourteen】
    【SA8295P 源码分析 (三)】Camera 模块 文章链接汇总 - 持续更新中
  • 原文地址:https://blog.csdn.net/zj850324/article/details/126470971