html视频---微信横竖屏

本文介绍了一种视频播放页面的设计方案,通过HTML5的video标签实现视频播放,并提供了竖屏播放、横屏播放及自动旋转的功能。代码中包含了对不同屏幕尺寸的适应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html lang="en"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="author" content="monicaqin">
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <title>测试视频test</title>
 
</head>

<body>
 
<div>
<video id="test_video" src="movie/luyu.mp4" controls="controls" x5-video-player-type="h5" x5-video-player-fullscreen="true" autoplay="" style="width: 1536px; height: 100%; display: block;"></video>
</div>

<div style="position:relative;top:-100px;">
        <input value="竖屏播放" onclick="portrait();" type="button">
        <input value="横屏播放" onclick="landscape();" type="button">
        <input value="跟随旋转" onclick="autoOrientation();" type="button">
</div>



<script type="text/javascript">
    test_video.style.width = screen.width + "px";
    test_video.style.height = "100%";    
</script>
            

<div>
<canvas id="test">
</canvas></div>


<script type="text/javascript">
window.onresize=function(){
    //alert("haha");
    test_video.style.width = screen.width;
    test_video.style.height = screen.height;
}

test_video.addEventListener("timeupdate", function(){
    if(test_video.currentTime > 2)
    {
        test_video.style.display = 'block';
    }
});

function testPlay(){
    test_video.play();
}


window.onresize=function(){
    //alert("haha");
    test_video.style.width = screen.width;
    test_video.style.height = screen.height;
}


function portrait(){
    test_video.setAttribute("x5-video-orientation", "portrait");
}

function landscape(){
    test_video.setAttribute("x5-video-orientation", "landscape");
}

function autoOrientation(){
    test_video.setAttribute("x5-video-orientation", "landscape|portrait")
}
</script>
         
 


</body></html>

不写注释的都是仅作为个人笔记,不懂可以留言。

### HTML5 Video 标签全屏播放与横竖屏适配解决方案 HTML5 的 `<video>` 标签提供了多种属性和方法,可以用来实现全屏播放以及适配横屏和屏的功能。以下是详细的解决方案: #### 1. 使用 `requestFullscreen` 方法实现全屏播放 现代浏览器支持通过 JavaScript 调用 `requestFullscreen()` 方法来让视频进入全屏模式。此方法适用于大多数主流浏览器。 ```javascript const videoElement = document.querySelector('video'); // 进入全屏模式 function enterFullScreen() { if (videoElement.requestFullscreen) { videoElement.requestFullscreen(); } else if (videoElement.mozRequestFullScreen) { // Firefox videoElement.mozRequestFullScreen(); } else if (videoElement.webkitEnterFullscreen) { // Safari videoElement.webkitEnterFullscreen(); } } ``` 需要注意的是,某些移动设备可能不完全支持标准的全屏 API,因此需要额外考虑兼容性问题[^1]。 --- #### 2. 设置 CSS 属性以适配屏幕方向 为了确保视频能够正确填充整个屏幕,无论处于横屏还是屏状态,可以通过设置 CSS 来调整视频的比例。 ```css video { width: 100%; height: auto; max-width: 100%; /* 确保视频不会超出父容器 */ } /* 当进入全屏时,强制拉伸至覆盖整个屏幕 */ @media (orientation: landscape) { video { width: 100vw; height: 100vh; object-fit: cover; /* 自动裁剪多余部分 */ } } @media (orientation: portrait) { video { width: 100vw; height: calc(100vw * 9 / 16); /* 维持常见比例 */ object-fit: contain; /* 完整显示内容 */ } } ``` 以上样式定义了视频在不同屏幕方向下的表现形式,并利用 `object-fit` 属性控制视频的内容缩放行为[^4]。 --- #### 3. 处理移动端特殊限制 在移动端(尤其是 iOS 和 Android 上嵌套 WebView 的场景),可能会遇到一些特殊的限制或问题,例如无法隐藏默认的全屏按钮或者无法自由操作 DOM 元素。针对这些问题,有以下几种解决办法: ##### (1)禁用微信内置 X5 浏览器中的默认全屏按钮 如果目标平台是基于微信环境,则可通过设置特定属性启用同层播放模式,从而移除右上角的“全屏”按钮并增强用户体验。 ```html <video src="example.mp4" controls x5-video-player-type="h5" x5-video-player-fullscreen="true"> </video> ``` 这些属性仅对腾讯 X5 内核有效,允许开发者更灵活地定制视频播放界面[^2]。 ##### (2)拦截 Android WebView 中的全屏事件 当使用 Android WebView 加载网页时,默认情况下点击全屏按钮会触发系统的原生处理流程。为了避免这种情况,可以在 Activity 中重写相关回调函数,接管全屏逻辑。 ```java @Override public void onShowCustomView(View view, CustomViewCallback callback) { super.onShowCustomView(view, callback); FrameLayout fullScreenContainer = findViewById(R.id.fl_video); fullScreenContainer.setVisibility(View.VISIBLE); fullScreenContainer.addView(view); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // 切换到横屏 } @Override public void onHideCustomView() { super.onHideCustomView(); FrameLayout fullScreenContainer = findViewById(R.id.fl_video); fullScreenContainer.removeAllViews(); fullScreenContainer.setVisibility(View.GONE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // 返回屏 } ``` 这段代码展示了如何捕获全屏视图的变化,并手动管理其生命周期[^3]。 --- #### 4. 动态检测屏幕方向并响应 为了让应用程序更加智能化,还可以借助 JavaScript 或者框架监听屏幕方向的变化,并实时调整 UI 布局。 ```javascript window.addEventListener("resize", () => { const isLandscape = window.innerWidth > window.innerHeight; if (isLandscape) { console.log("当前为横屏"); // 执行横屏专属逻辑 } else { console.log("当前为屏"); // 执行屏专属逻辑 } }); ``` 这种方法特别适合那些需要根据不同方向加载差异化资源的应用程序。 --- ### 总结 综合来看,要实现跨平台、高质量的 HTML5 视频全屏播放体验,需结合前端技术栈的特点分别应对桌面端与移动端的不同挑战。具体而言,应优先尝试标准化接口如 `requestFullscreen()`;同时辅以针对性优化策略——比如配置专属于微信生态的扩展参数或是深入干预 Android WebView 行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值