自己怎样对video播放器进行封装,哪些要注意的
Admin 2022-05-30 群英技术资讯 1518 次浏览
这篇文章主要讲解了“自己怎样对video播放器进行封装,哪些要注意的”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“自己怎样对video播放器进行封装,哪些要注意的”吧!当现有video播放器不能满足需求时,需要自己对video进行封装。
HLS(HTTP Live Streaming)由Apple提出的直播流协议。IOS和高版本Android都支持HLS。HLS主要由.m3u8和.ts两种播放文件。HLS具有高兼容性,高可扩展性,但会直播延时。HLS协议是将直播流分成一段一段的小段视频去下载播放的,所以假设列表里面的包含5个ts文件,每个ts文件包含5秒的视频内容,那么整体的延迟就是25秒。
RTMP(Real Time Messaging Protocol)是Macromedia开发的一套视频直播协议,现在属于Adobe。RTMP基于flash无法在IOS的浏览器里播放,但是实时性比HLS要好。
HTTP-FLV针对于FLV视频格式做的直播分发流,延时低。但移动端不支持。
结论:HTTP-FLV延时低,优先使用。若设备不支持HTTP-FLV,使用flv.js。若设备不支持flv.js,则使用HLS,但HLS延迟大。
/** HTML **/
<div class="video">
<!-- video player -->
<video
class="video__player"
ref="videoPlayer"
preload="auto"
:autoplay="options.autoplay"
:muted="options.muted"
webkit-playsinline="true"
playsinline="true"
x-webkit-airplay="allow"
x5-video-player-type="h5-page"
x5-video-orientation="portraint"
style="object-fit:cover;"
>
<source :src="options.src" />
</video>
<!-- video poster -->
<div
v-show="showPoster"
class="video__poster"
:style="{'background-image': 'url(' + options.pic + ')'}"
></div>
<!-- video loading -->
<div v-show="showLoading" class="video__Loading">
<span class="video__Loading-icon"></span>
</div>
<!-- video pause -->
<div @click="handleVideoPlay" class="video__pause">
<span v-show="!videoPlay" class="video__pause-icon"></span>
</div>
</div>
/** js**/
props: {
options: {
type: Object,
default: () => {}
}
},
data() {
return {
videoPlay: false, // 是否正在播放
videoEnd: false, // 是否播放结束
showPoster: true, // 是否显示视屏封面
showLoading: false, // 加载中
currentTime: 0, // 当前播放位置
currentVideo: {
duration: this.options.duration
},
}
},
mounted() {
this.videoPlayer = this.$refs.videoPlayer;
this.videoPlayer.currentTime = 0;
this.videoPlayer.addEventListener("loadstart", e => {
this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
});
// 获取到视频长度
this.videoPlayer.addEventListener("durationchange", e => {
this.currentVideo.duration = this.videoPlayer.duration;
// 存在播放历史记录
this.videoPlayer.currentTime = (this.options.playProcess > 0) ? this.options.playProcess : 0;
});
this.videoPlayer.addEventListener("playing", e => {
this.showPoster = false;
this.videoPlay = true;
this.showLoading = false;
this.videoEnd = false;
});
// 暂停
this.videoPlayer.addEventListener("pause", () => {
this.videoPlay = false;
if (this.videoPlayer.currentTime < 0.1) {
this.showPoster = true;
}
this.showLoading = false;
});
// 播放进度更新
this.videoPlayer.addEventListener("timeupdate", e => {
this.currentTime = this.videoPlayer.currentTime;
},
false
);
// 指定播放位置
this.videoPlayer.addEventListener("seeked", e => {
});
// 缓冲
this.videoPlayer.addEventListener("waiting", e => {
this.showLoading = true;
});
// 播放结束
this.videoPlayer.addEventListener("ended", e => {
// 重置状态
this.videoPlay = false;
this.showPoster = true;
this.videoEnd = true;
this.currentTime = this.currentVideo.duration;
});
// 监听weixinJSBridgeReady事件,处理微信不能自动播放。但并不全部适用,加了暂停按钮,手动播放。
document.addEventListener('WeixinJSBridgeReady', () => {
this.videoPlayer.play();
}, false);
},
methods: {
handleVideoPlay() {
if (this.videoPlayer.paused) { // 播放
if(this.videoEnd) { // 重播
this.currentTime = 0;
this.videoPlayer.currentTime = 0;
}
this.videoPlayer.play();
this.videoPlay = true;
} else { // 暂停
this.videoPlayer.pause();
this.videoPlay = false;
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了vue实现搜索小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
JS实现懒加载图片方法有哪些?实现懒加载图片方法有很多,这篇文章就给大家分享三种利用原生JS实现懒加载lazyLoad的方法以及对比,下文实例对大家理解JS实现懒加载图片有一定的帮助,下面我们一起来看看吧。
这篇文章主要给大家介绍了关于JS对象数组去重的3种方法,三种方法分别包括使用filter和Map、使用reduce以及for循环,文中每个方法都给出了示例代码,需要的朋友可以参考下
我们在用电脑浏览一些商城网站的时候,将鼠标移至商品图片就有放大查看细节的功能,很多朋友比较好奇这样放大镜的功能是怎样实现的,因此这篇文章就主要给大家分享用JS实现放大镜查看商品细节功能,实现效果的代码如下:
canvas有一个非常常用的方法canvas.toDataURL(),它会将canvas转化为data URL的格式。通常情况下这个data URL的类型为image。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008