uni-app 微信公众号H5开发 里面的video 组件点击全屏后不会自动横屏播放


前言

video 组件 视频播放,在微信浏览器中点击全屏的时候不会自动横屏。客户说别人的为什么能自动横屏你的为什么不能,爆肝了两天,百度了一篇又一篇,什么x5属性往上加,pi用没有,然后看见一个内置旋转的方案,莫得办法,就用这个解决一下,好像做了又好像没做


一、不能自动横屏的原因?

1.微信内置的设置横屏模式没开启
解决方案,让用户去开启微信的横屏模式,但是甲方不愿意,弃用

2.微信浏览器用的是X5核心
解决方案,让用户去微信聊天窗口发送 debugmm.qq.com/?forcex5=false
然后在点击,自动关闭X5。但是客户第一个解决方案都不愿意,这个方案是在想pi吃

3.内置旋转,但是微信公众号H5的导航栏,遮罩是遮罩不住的,伪全屏?

二、在里就奉上代码

H5的代码

<view class="video-box" ref="videoBox">
			<video class="video" id="myVideo" controls :src="itemInfo.url" :poster="itemInfo.bgUrl"
				style="object-fit:fill">
			</video>
			<view class="ComedyScreenBox">
				<!-- 全屏按钮 -->
				<view class="ComedyScreen" @click.stop="ComedyScreen(1)" v-show="!ComedyScreenStatus">
					<image src="../../../static/images/index/screen.png" mode=""></image>
				</view>
				<!-- 退出全屏按钮 -->
				<view class="ComedyReturn" @click.stop="ComedyScreen(0)" v-show="ComedyScreenStatus">
					<image src="../../../static/images/index/screen1.png" mode=""></image>
				</view>
			</view>
		</view>

CSS样式

.video-box {
	width: 750rpx;
	height: 422rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 999;
}
.video {
	width: 750rpx;
	height: 422rpx;
}
#myVideo {
	width: 100%;
	height: 211px;
	z-index: 999;
	display: flex;
	align-items: center;
	justify-content: center;
	position: relative;
}
.ComedyScreenBox {
	z-index: 1000;
	position: absolute;
	right: 0;
	top: 0;
	width: 20px;
	height: 20px;
	padding: 5px;
}
.ComedyScreenBox image{
	width: 64rpx;
	height: 64rpx;
}

.ComedyScreen {
	width: 100%;
	height: 100%;
	background-repeat: no-repeat;
	cursor: pointer;
	position: absolute;
	right: 0;
	transform: rotate(90deg);
}

.ComedyReturn {
	width: 100%;
	height: 100%;
	cursor: pointer;
	position: absolute;
	right: 0;
	transform: rotate(90deg);
}

js

	onShow() {
		let self = this
		window.addEventListener('orientationchange', function(event) {
			self.orientation=window.orientation
			//监听手机方向  竖屏
			if (window.orientation == 180 || window.orientation == 0) {
				self.left = false
				self.ComedyScreen()

				// if (!that.ComedyScreenStatus) {
				// 	self.ComedyScreen(0, 'top')
				// }



			}
			//监听手机方向  横屏
			if (window.orientation == 90 || window.orientation == -90) {
				self.left = true
				self.ComedyScreen(1,true)
			}
		});
	},
	methods: {
			ComedyScreen(status, title) {

			console.log(status)
			let _this = this;
			if (status) {
				this.$nextTick(function() {
					let that = this;
					uni.getSystemInfo({
						success: function(res) {

							console.log(that.orientation)
							if (title || that.orientation == 90 || that.orientation == -90) {
							// 监听手机横屏的时候 自动给变成横屏方向播放
								that.$refs.videoRef.$el.style.width = "100%";
								that.$refs.videoRef.$el.style.height = "100%";
								that.$refs.videoRef.$el.style.position = "absolute";
								let top = (res.windowHeight - res.screenWidth) / 2;
								that.$refs.videoBox.$el.style.width = "100%";
								that.$refs.videoBox.$el.style.height = "100%";
								that.$refs.videoBox.$el.style.position = "fixed";
								that.$refs.videoBox.$el.style.top = 0;
								that.$refs.videoBox.$el.style.left = 0;
								that.$refs.videoBox.$el.style.right = 0;
								that.$refs.videoBox.$el.style.bottom = 0;
								that.$refs.videoBox.$el.style.transform = "rotate(0deg)";
								that.ComedyScreenStatus = true;
							} else {
							//点击全屏播放按钮走的逻辑
								that.$refs.videoRef.$el.style.width = res.windowHeight + "px";
								that.$refs.videoRef.$el.style.height = res.windowWidth + "px";
								that.$refs.videoRef.$el.style.position = "absolute";
								let top = (res.windowHeight - res.screenWidth) / 2;
								that.$refs.videoBox.$el.style.width = res.windowHeight + "px";
								that.$refs.videoBox.$el.style.height = res.windowWidth + "px";
								that.$refs.videoBox.$el.style.position = "fixed";
								that.$refs.videoBox.$el.style.top = top + "px";
								that.$refs.videoBox.$el.style.left = -top + "px";
								that.$refs.videoBox.$el.style.transform = "rotate(90deg)";
								that.ComedyScreenStatus = true;
							}
						}
					});
				});
			}
			if (!status) {
			//退出全屏,或者竖屏进入的方法
				this.$nextTick(function() {
					let that = this;
					uni.getSystemInfo({
						success: function(res) {
							console.log(that.$refs.videoRef, that.$refs)
							that.$refs.videoRef.$el.style.width = "100%";
							that.$refs.videoRef.$el.style.height = "211px";
							that.$refs.videoRef.$el.style.position = "relative";
							that.$refs.videoBox.$el.style.width = "100%";
							that.$refs.videoBox.$el.style.height = "211px";
							that.$refs.videoBox.$el.style.position = "fixed";
							that.$refs.videoBox.$el.style.top = "0px";
							that.$refs.videoBox.$el.style.left = "0px";
							that.$refs.videoBox.$el.style.transform = "rotate(0deg)";
							that.ComedyScreenStatus = false;
							that.left = false
						}
					});
				});
			}
		},
	}

总结

效果很差,微信公众号自带的导航栏,遮不住,好像也要去开启横屏模式?所以写了个寂寞,拿去看看就好,用这个写出来甲方那边应该也不同意,真的越写越自闭,所以在线等解决方案
欢迎大家评论交流。

uni-app微信小程序、H5App等多端统一框架)中,如果你尝试通过简单的`<video>`标签直接插入视频源地址而无法播放,可能遇到以下几个问题: 1. **权限设置**:首先检查项目配置是否允许访问网络资源,微信小程序需要在config.json中配置"mediaDomain"。 ```json { "pages": [...], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "uni-app", "navigationBarTextStyle": "black", "mediaDomain": "你的域名" } } ``` 2. **URL格式**:确认视频地址格式正确,uni-app支持.mp4、.flv、.webm等常见的视频格式。对于跨域问题,确保源地址可以被uni-app所用。 3. **兼容性问题**:uni-app有时可能需要处理不同环境下的播放器兼容性,如iOS的全屏模式。你可以考虑引入第三方库(如h5-video-player)增强兼容性。 4. **编码问题**:如果视频编码格式不被浏览器或小程序引擎支持,也可能导致播放失败,需要转换为合适的格式。 5. **错误处理**:检查页面加载时是否有报错信息,uni-app有自己的错误捕获机制,可以在生命周期钩子函数中监听并处理。 如果以上都排查过了还是无法解决,可以尝试添加`playsinline="true"`属性,让视频在非全屏环境下播放,并且在JavaScript中控制视频的play()和pause()方法,例如: ```html <video id="myVideo" playsinline src="your_video_url" ></video> <script> var video = document.getElementById('myVideo'); video.play(); </script> ```
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值