DCloud_UNI_GSQ
DCloud_UNI_GSQ
  • 发布:2019-01-02 21:04
  • 更新:2024-04-01 16:18
  • 阅读:39971

uni-app在H5平台使用video.js,以及播放m3u8格式的视频

分类:uni-app
  1. 创建模板文件index.html,在模版内引用video.js
<!DOCTYPE html>  
<html lang="zh-CN">  
    <head>  
        <meta charset="utf-8">  
        <meta http-equiv="X-UA-Compatible" content="IE=edge">  
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">  
        <title>  
            <%= htmlWebpackPlugin.options.title %>  
        </title>  
        <script>  
            document.addEventListener('DOMContentLoaded', function() {  
                document.documentElement.style.fontSize = document.documentElement.clientWidth / 20 + 'px'  
            })  
        </script>  
        <link rel="stylesheet" href="<%= htmlWebpackPlugin.options.baseUrl %>static/index.css" />  
        <link href="https://unpkg.com/video.js/dist/video-js.min.css" rel="stylesheet">  
        <script src="https://unpkg.com/video.js/dist/video.min.js"></script>  
    </head>  
    <body>  
        <noscript>  
            <strong>Please enable JavaScript to continue.</strong>  
        </noscript>  
        <div id="app"></div>  
        <!-- built files will be auto injected -->  
    </body>  
</html>
  1. 在manifest.json内配置index.html路径
  2. 在vue文件内使用video.js初始化播放器
<template>  
    <view class="content">  
        <view class="video-js" ref="video">  

        </view>  
    </view>  
</template>  

<script>  
    export default {  
        onReady() {  
            // #ifdef H5  
            var video = document.createElement('video')  
            video.id = 'video'  
            video.style = 'width: 300px;height: 150px;'  
            video.controls = true  
            var source = document.createElement('source')  
            source.src = 'http://yf.ugc.v.cztv.com/cztv/ugcvod/2018/04/14/A98CD7B26B06D94A5CEA56AA7D723572/h264_800k_mp4.mp4_playlist.m3u8'  
            video.appendChild(source)  
            this.$refs.video.$el.appendChild(video)  
            videojs('video')  
            // #endif  
        },  
        methods: {  

        }  
    }  
</script>  

<style>  

</style>

注意事项

  1. 示例工程见附件
  2. 如果视频源格式为m3u8,m3u8文件可能遇到跨域问题,请自行百度
9 关注 分享
Trust Stalker丶 dub 3***@qq.com 子衣 锦笑江湖 h***@163.com 2***@qq.com 9***@qq.com

要回复文章请先登录注册

DCloud_UNI_GSQ

DCloud_UNI_GSQ (作者)

回复 y***@163.com :
下载下来放到static目录
2020-08-19 12:03
y***@163.com

y***@163.com

项目在公安网,没法引用网络的库,如果要用本地videojs库要怎么做?
2020-08-18 17:12
DCloud_UNI_GSQ

DCloud_UNI_GSQ (作者)

回复 y***@qq.com :
如果视频源格式为m3u8,m3u8文件可能遇到跨域问题,请自行百度
2020-07-22 16:16
y***@qq.com

y***@qq.com

试用了,安卓可以,ios 不行,有解决办法吗。
2020-07-22 11:18
kimshell

kimshell

Error in onReady hook: "TypeError: Cannot read property '$el' of undefined"
请问这是怎么回事啊
2020-07-10 13:28
youngswelli

youngswelli

回复 1***@163.com :
请重启项目!
2020-05-31 22:24
1***@163.com

1***@163.com

回复 1***@163.com :
还会报错
video.min.js:12 VIDEOJS: ERROR: DOMException: Failed to read the 'buffered' property from 'SourceBuffer': This SourceBuffer has been removed from the parent media source.
2020-05-28 01:41
1***@163.com

1***@163.com

报错看不懂,同样是m3u8的格式,有的就会报错,视频地址http://1251581441.vod2.myqcloud.com/05a0b002vodcq1251581441/1a3d90415285890803512045911/playlist_eof.m3u8,报错截图
{code: 4
message: "The media could not be loaded, either because the server or network failed or because the format is not supported."}
2020-05-28 01:40
1***@163.com

1***@163.com

回复 DCloud_UNI_GSQ :
这个我怎么拿到video的实例然后调用方法啊?比如:this.videoPlayer.play() 或 this.videoPlayer.pause() ???
2020-05-27 11:57
DCloud_UNI_GSQ

DCloud_UNI_GSQ (作者)

回复 1***@163.com :
模板html里你没加videojs吧,看下示例
2020-05-27 11:43