HTML5   发布时间:2022-04-26  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了html5-video – 如何使用Coverr的代码解决html5视频无法播放的问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经从 Coverr上传一个视频,并将HTML,CSS和JS添加到了我的主页.

但是,视频无法播放.

HTML:

<div class="homepage-hero-module">
    <div class="video-container">
        <div class="filter"></div> 
        <video autoplay loop class="fillWidth"> 
            <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.mp4" type="video/mp4" ></source>Your browser does not support the video tag. I suggest you upgrade your browser. 
            <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.webm" type="video/webm" ></source>Your browser does not support the video tag. I suggest you upgrade your browser.
        </video>
        <div class="poster hidden"><img src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.jpg" alt="Escalate your Business Internet success with the Help of a web professional"></div> 
    </div>
</div>

CSS:

<style>
.homepage-hero-module {
    border-right: none;
    border-left: none;
    position: relative;
}
.no-video .video-container video,.touch .video-container video {
    display: none;
}
.no-video .video-container .poster,.touch .video-container .poster {
    display: block !important;
}
.video-container {
    position: relative;
    bottom: 0%;
    left: 0%;
    height: 100%;
    width: 100%;
    overflow: hidden;
    BACkground: #000;
}
.video-container .poster img {
    width: 100%;
    bottom: 0;
    position: absolute;
}
.video-container .filter {
    z-index: 100;
    position: absolute;
    BACkground: rgba(0,0.4);
    width: 100%;
}
.video-container video {
    position: absolute;
    z-index: 0;
    bottom: 0;
}
.video-container video.fillWidth {
    width: 100%;
}
</style>

JS:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
//jQuery is required to run this code
jQuery( document ).ready(function() {

    scaleVideoContainer();

    initBAnnerVideoSize('.video-container .poster img');
    initBAnnerVideoSize('.video-container .filter');
    initBAnnerVideoSize('.video-container video');

    jQuery(window).on('resize',function() {
        scaleVideoContainer();
        scaleBAnnerVideoSize('.video-container .poster img');
        scaleBAnnerVideoSize('.video-container .filter');
        scaleBAnnerVideoSize('.video-container video');
    });

});

function scaleVideoContainer() {

    var height = jQuery(window).height() + 5;
    var unitHeight = parseInt(height) + 'px';
    jQuery('.homepage-hero-module').css('height',unitHeight);

}

function initBAnnerVideoSize(element){

    jQuery(element).each(function(){
        jQuery(this).data('height',jQuery(this).height());
        jQuery(this).data('width',jQuery(this).width());
    });

    scaleBAnnerVideoSize(element);

}

function scaleBAnnerVideoSize(element){

    var windowWidth = jQuery(window).width(),windowHeight = jQuery(window).height() + 5,videoWidth,videoHeight;

    console.log(windowHeight);

    jQuery(element).each(function(){
        var videoAspectRatio = jQuery(this).data('height')/jQuery(this).data('width');

        jQuery(this).width(windowWidth);

        if(windowWidth < 1000){
            videoHeight = windowHeight;
            videoWidth = videoHeight / videoAspectRatio;
            jQuery(this).css({'margin-top' : 0,'margin-left' : -(videoWidth - windowWidth) / 2 + 'px'});

            jQuery(this).width(videoWidth).height(videoHeight);
        }

        jQuery('.homepage-hero-module .video-container video').addClass('fadeIn animated');

    });
}
</script>

我在控制台中看不到任何错误,因此我不知道为什么视频无法在最新版本的Chrome或Firefox中播放.

帮助赞赏.

解决方法

您的视频实际上正在播放,但它隐藏在您的海报div下.除非你有另一个理由为海报使用单独的div,你应该虑使用视频元素的poster属性,如下所示:
<video autoplay loop class="fillWidth"  poster="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.jpg"> 
        <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.mp4" type="video/mp4" ></source> 
        <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.webm" type="video/webm" ></source>Your browser does not support the video tag. I suggest you upgrade your browser.
    </video>

但是如果你有自动播放属性,则不需要海报,除非视频加载缓慢.

要使用文本注释视频,您可以执行以下操作:

<div class="video-container">
    <div class="filter"></div> 
    <video loop class="fillWidth"  poster="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.jpg"> 
        <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.mp4" type="video/mp4" ></source>
        <source src="https://doig.website.technology/wp-content/uploads/2016/09/Escalate.webm" type="video/webm" ></source>Your browser does not support the video tag. I suggest you upgrade your browser.
    </video>
    <div class="video-Annotation-layer">
      <div id="video-Annotation-1">Hello there!!</div>
      <div id="video-Annotation-2">Hey buddy!!</div></div>
</div>

然后在CSS中:

.video-Annotation-layer{
    width:100%;
    height:100%;
}
#video-Annotation-1{
    position:absolute;
    top:40%;
    left:40%;
    font-size:50px;
    text-align:center;
}
#video-Annotation-2{
    position:absolute;
    top:60%;
    left:30%;
    color:blue;
    BACkground-color:white;
    font-size:40px;
    text-align:center;
}

大佬总结

以上是大佬教程为你收集整理的html5-video – 如何使用Coverr的代码解决html5视频无法播放的问题全部内容,希望文章能够帮你解决html5-video – 如何使用Coverr的代码解决html5视频无法播放的问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。