zoukankan      html  css  js  c++  java
  • 微信视频播放全屏问题

    在ios和安卓手机里的微信下播放视频时,会遇到不少问题,例如需要手动点击,视频才会播放,并且视频会跳出微信框,出现控制条,如果视频不是腾讯视频,播放完毕会出现腾讯视频的广告推送,今天我们就来看一下如何规避这些问题。

    解决办法:给video标签加一些属性,调用h5原生video。

    下面是我写的一个小例子的html核心部分:

    <video
      id="videoALL" 
      src="video/01.mp4" 
      poster="images/1.jpg" /*视频封面*/
      preload="auto" 
      webkit-playsinline="true" /*这个属性是ios 10中设置可以让视频在小窗内播放,也就是不是全屏播放*/  
      playsinline="true"  /*IOS微信浏览器支持小窗内播放*/ 
      x-webkit-airplay="allow" 
      x5-video-player-type="h5"  /*启用H5播放器,是wechat安卓版特性*/
      x5-video-player-fullscreen="true" /*全屏设置,设置为 true 是防止横屏*/>
      x5-video-orientation="portraint" /*播放器支付的方向,landscape横屏,portraint竖屏,默认值为竖屏*/
      style="object-fit:fill">
    </video>

    下面我们来看看这些属性的作用:

    poster="images/1.jpg":属性规定视频下载时显示的图像,或者在用户点击播放按钮前显示的图像。如果未设置该属性,则使用视频的第一帧来代替。

    preload="auto" 属性规定在页面加载后载入视频。

    webkit-playsinline和playsinline:视频播放时局域播放,不脱离文档流 。但是这个属性比较特别, 需要嵌入网页的APP比如WeChat中UIwebview 的allowsInlineMediaPlayback = YES webview.allowsInlineMediaPlayback = YES,才能生效。换句话说,如果APP不设置,你页面中加了这标签也无效,这也就是为什么安卓手机WeChat 播放视频总是全屏,因为APP不支持playsinline,而ISO的WeChat却支持。

    这里就要补充下,如果是想做全屏直播或者全屏H5体验的用户,ISO需要设置删除 webkit-playsinline 标签,因为你设置 false 是不支持的 ,安卓则不需要,因为默认全屏。但这时候全屏是有播放控件的,无论你有没有设置control。 做直播的可能用得着播放控件,但是全屏H5是不需要的,那么去除全屏播放时候的控件,需要以下设置:同层播放。

    x-webkit-airplay="allow"暂时无法确切的知道其作用,但是小编猜测,这个属性应该是使此视频支持ios的AirPlay功能。使用AirPlay可以直接从使用iOS的设备上的不同位置播放视频、音乐还有照片文件,也就是说通过AirPlay功能可以实现影音文件的无线播放,当然前提是播放的终端设备也要支持相应的功能。

    x5-video-player-type启用同层H5播放器,就是在视频全屏的时候,div可以呈现在视频层上,也是WeChat安卓版特有的属性。同层播放别名也叫做沉浸式播放,播放的时候看似全屏,但是已经除去了control和微信的导航栏,只留下"X"和"<"两键。目前的同层播放器只在Android(包括微信)上生效,暂时不支持iOS。至于为什么同层播放只对安卓开放,是因为安卓不能像ISO一样局域播放,默认的全屏会使得一些界面操作被阻拦,如果是全屏H5还好,但是做直播的话,诸如弹幕那样的功能就无法实现了,所以这时候同层播放的概念就解决了这个问题。不过在测试的过程中发现,不同版本的ISO和安卓效果略有不同。

    x5-video-orientation声明播放器支持的方向,可选值landscape 横屏, portraint竖屏。默认值portraint。无论是直播还是全屏H5一般都是竖屏播放,但是这个属性需要x5-video-player-type开启H5模式

    x5­-video­-player­-fullscreen全屏设置。它又两个属性值,ture和false,true支持全屏播放,false不支持全屏播放。

    其实,ISO 微信浏览器是Chrome的内核,相关的属性都支持,也是为什么X5同层播放不支持的原因。安卓微信浏览器是X5内核,一些属性标签比如playsinline就不支持,所以始终全屏。

       还有个问题,在Android的微信里面,就算加上了上面的属性,还会出现上下有黑边,不能全屏的问题。

    解决办法:给video加上object-fit: fill;的style属性。如果还是有黑边有可能是视频尺寸不合适。

       而且小编发现视频在打开的瞬间,会出现很明显的放缩闪屏问题,只需要给视频设置一个合适的宽高就可以解决啦。

                                                                                    DEMO演示

                                                                          

    DEMO在iOS和Android下的样式

    核心代码如下:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
    <meta name="format-detection"content="telephone=no">
    <title>微信下视频全屏播放问题</title>
    <link href="style/reset.css" type="text/css" rel="stylesheet" />
    <style>
        #videobox{position: absolute;width: 100%;height: 100%;background-color: green;background-image: url(images/1.jpg);background-size: 100% 100%;background-position: top;overflow: hidden;}
        #videoALL{
      height: auto;
      position: absolute;
      width: 100%;
      top: 0;
      left: 0;object-fit: fill;
      display: block;background-size: cover;overflow: hidden;}
        #btn,#againbtn{width: 81px;height: 75px;position: absolute;top: 50%;left:50%;margin-top: -37.5px;margin-left: -40.5px;background-image: url(images/btn.png);background-size: 100% 100%;}
        #videoend{position: absolute;background-color: pink;display: none;background-image: url(images/2.jpg);background-size: cover;background-position: top;}
    </style>
    </head>
    
    <body>
    <div id="videobox">
       <video 
        id="videoALL" 
        src="video/01.mp4" 
        poster="images/1.jpg" 
        preload="auto" 
        webkit-playsinline="true" 
        playsinline="true" 
        x-webkit-airplay="allow" 
        x5-video-player-type="h5" 
        x5-video-player-fullscreen="true" 
         x5-video-orientation="portraint"
        style="object-fit:fill">
        </video> 
    
       <div id="btn" onclick="playcontr()"></div>
    </div>
    
    <div id="videoend"><div id="againbtn" onclick="playcontr()"></div></div>
    
    <script>
    var videoALL = document.getElementById('videoALL'),
        videobox = document.getElementById('videobox'),
        btn = document.getElementById('btn'),
        videoend =  document.getElementById('videoend');
    var clientWidth = document.documentElement.clientWidth;
    var clientHeight = document.documentElement.clientHeight;
    videoALL.style.width = clientWidth + 'px';
    videoALL.style.height = 'auto';
    document.addEventListener('touchmove', function(e){e.preventDefault()}, false);
    
    function stylediv(divId){
        divId.style.width = clientWidth + 'px';
        divId.style.height = clientHeight + 'px'; 
    }
    stylediv(videobox);
    stylediv(videoend);
    
    var u = navigator.userAgent; 
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端 
    var isiOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 
    
    function playcontr(){
        if (isAndroid) {
           videoALL.style.width = window.screen.width + 'px';
           videoALL.style.height = window.screen.height + 'px'; 
        }
        videobox.style.display = "block";
        videoALL.play();
        btn.style.display = "none";
        videoend.style.display = "none";
    };
    
    videoALL.addEventListener('pause',function(){  
        videoALL.style.width = clientWidth + 'px';
        btn.style.display = "block";
    })  
    
    videoALL.addEventListener("ended",function(){
        videoALL.pause();
        videobox.style.display = "none";
        videoend.style.display = "block";
    });
    
    </script>
    </body>
    </html>

    此文章主要发布在本人所在公司网站H5案例分享(http://www.h5-share.com/)、公司公众号H5握手和个人博客中,转载请注明出处。

  • 相关阅读:
    工厂模式
    dubbo
    WebSocket WebService
    消息中间
    原型模式
    ApiPost Apifox
    Future 的使用与源码解析
    JUC 线程池的使用与源码解析
    ReentrantLock 源码解析
    CountDownLatch 的使用与源码解析
  • 原文地址:https://www.cnblogs.com/daipianpian/p/6545854.html
Copyright © 2011-2022 走看看