我这里是uniapp开发的H5项目
视频流是flv模式
用到的插件是flv.js
Flv.js 是 HTML5 Flash 视频(FLV)播放器,纯原生 JavaScript 开发,没有用到 Flash。。由 bilibili 网站开源。
架构描述:
上代码:
<template>
<!-- 直播 -->
<view class="mylive flex align_center">
<view class="sp_live">
<view class="video-js flex" ref="video"> </view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
videoDeviceId: '',
flvPlayer: '',
VideoDeviceUrl: ''
}
},
onLoad: function(option) {
this.VideoDeviceUrl = decodeURIComponent(option.liveUrl)
// #ifdef H5
this.$nextTick(res => {
this.videoPush()
})
// #endif
},
onUnload: function() {
// #ifdef H5
this.flv_destroy()
// #endif
},
methods: {
videoPush: function() {
var video = document.createElement('video')
video.id = 'video'
video.style = 'width: 100%;'
video.controls = true
this.flvPlayer = flvjs.createPlayer({
type: 'flv',
isLive: true,
url: this.VideoDeviceUrl
});
this.flvPlayer.attachMediaElement(video);
this.flvPlayer.load();
this.flvPlayer.play();
this.$refs.video.$el.appendChild(video)
},
//注销视频
flv_destroy() {
this.flvPlayer.pause();
this.flvPlayer.unload();
this.flvPlayer.detachMediaElement();
this.flvPlayer.destroy();
this.flvPlayer = null;
}, }
}
</script>
但是在我项目中遇到一个问题就是,使用这个插件的全屏的时候(这个全屏功能好像在电脑上是页面全屏,视频没有全屏),ios设备不支持全屏,并且安卓设备全屏的时候也有问题,所有我就自己设置了一个点击事件,将页面顺时针旋转90°来模拟全屏。
下面是旋转的点击事件代码:
<button v-if="showBtn" id="rotate-btn" class="rotateBtn" @click="rotates">
旋转<u-icon style="margin-left: 2rpx;" name="reload"></u-icon>
</button>
rotates(){
const rotateElement = document.getElementById('container');
const rotateBtnElement = document.getElementById('rotate-btn');
if (rotateElement.classList.contains('initial')) {
rotateElement.classList.remove('initial');
rotateElement.classList.add('rotated');
} else {
rotateElement.classList.remove('rotated');
rotateElement.classList.add('initial');
}
},