当前位置:首页 > 技术分享

videojs前端播放m3u8格式视频

admin2年前 (2024-04-30)技术分享4827
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>前端播放m3u8格式视频</title>
    <link href="https://cdn.jsdelivr.net/npm/video.js@7.8.3/dist/video-js.min.css" rel="stylesheet">
    <script src='https://cdn.jsdelivr.net/npm/video.js@7.8.3/dist/video.min.js'></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/videojs-contrib-hls/5.15.0/videojs-contrib-hls.min.js" type="text/javascript"></script>
    <!-- 
    videojs-contrib-hls 用于在电脑端播放 如果只需手机播放可以不引入
    https://blog.csdn.net/u014696856/article/details/135365211
     -->
</head>
<body>
    <style>
        .video-js .vjs-tech {position: relative !important;}
    </style>
    <div>
        <video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" data-setup='{}' style='width: 100%;height: auto'>
            <source id="source" src="https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_fmp4/master.m3u8" type="application/x-mpegURL"></source>
        </video>
    </div>
    <div style="width:100px;height: 100px;background: red;margin:0 auto;line-height: 100px;color:#fff;text-align: center">切换视频</div>
</body>
 
<script>
    // videojs 简单使用
    var myVideo = videojs('myVideo', {
        bigPlayButton: true,
        textTrackDisplay: false,
        posterImage: false,
        errorDisplay: false,
    })
    myVideo.play()
    var changeVideo = function (vdoSrc) {
        if (/\.m3u8$/.test(vdoSrc)) { //判断视频源是否是m3u8的格式
            myVideo.src({
                src: vdoSrc,
                type: 'application/x-mpegURL' //在重新添加视频源的时候需要给新的type的值
            })
        } else {
            myVideo.src(vdoSrc)
        }
        myVideo.load();
        myVideo.play();
    }
    var src = 'https://test-streams.mux.dev/x36xhzz/x36xhzz.m3u8';
    document.querySelector('.qiehuan').addEventListener('click', function () {
        changeVideo(src);
    })
</script></html>


扫描二维码推送至手机访问。

版权声明:本文由小刚刚技术博客发布,如需转载请注明出处。

本文链接:https://blog.bitefu.net/post/591.html

分享给朋友:

“videojs前端播放m3u8格式视频” 的相关文章

Chrome 开启自带多线程下载支持 提升下载速度

Chrome 开启自带多线程下载支持 提升下载速度

Chrome 是当前性能最高的浏览器之一,但在下载速度方面,Chrome 可一点都不快。当前 Chrome 的下载仍是单线程机制,速度非常慢。对此,大家不得不使用第三方的下载工具,例如迅雷、IDM 等支持多线程下载的下载软件,来提高下载速度…

mysql update不支持set子查询更新 的解决办法

mysql update不支持set子查询更新 的解决办法

先看示例:SELECT uin,account,password,create_user_uin_tree FROM sys_user结果:表中的create_user_uin_tree标识该条记录由谁创建。创…

php高效检测远程图片是否存在

php高效检测远程图片是否存在function img_exits($url){     $ch = curl_init();    &…

apicloud影视APP源码 无需后台

apicloud影视APP源码 无需后台

介绍集合vip影视接口到一个android app中 方便观看各平台影视资源及直播开源地址:https://gitee.com/web/vip_yingshi软件架构使用apicloud搭建影视APP源码,无后台,调用接口同步api解析网址…

全世界最快dns分享 Public DNS Server List

偶然发现一个共享全世界最快dns的网站,当然也包含中国了https://public-dns.info/ 中国https://public-dns.info/nameserver/cn.html…

msixbundle怎么打开?win10后缀.MSIX安装包怎么安装使用

msixbundle怎么打开?win10后缀.MSIX安装包怎么安装使用

扩展名为 .msix的文件是Windows应用程序安装程序。微软推出的MSIX 作为EXE,MSI甚至AppX 软件包的替代品。在Windows 10中打开一个.MSIX 文件并开始安装打包的应用程序,只需双击该文件即可。但有也有不少朋友会…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。