热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法在IE和iOS中更改html5视频黑条的颜色-Cannotchangecolorofhtml5videoblackbarsinIEandiOS

Iamattemptingtodisplayavideoinaresponsivedesignsuchthatthescalingbordersblendintot

I am attempting to display a video in a responsive design such that the scaling borders blend into the background.

我试图在响应式设计中显示视频,以便缩放边框融入背景。

I allow the dimensions of the video element to vary within specified bounds. As a result, when the video playing doesn't fill the actual html element, I get the black padding bars around my video.

我允许视频元素的尺寸在指定的范围内变化。因此,当视频播放没有填充实际的html元素时,我的视频周围会出现黑色填充条。

Using the css background property I have been able to change the color of the bars shown in Chrome, FireFox, and Opera. I cannot figure out how to change the color shown for Internet Explorer or iOS (ipad).

使用css background属性,我可以更改Chrome,FireFox和Opera中显示的条形颜色。我无法弄清楚如何更改Internet Explorer或iOS(ipad)显示的颜色。

Can anyone help me out with this?

任何人都可以帮我解决这个问题吗?

fiddle as requested: http://jsfiddle.net/swaEe/

根据要求提供小提琴:http://jsfiddle.net/swaEe/

html:



css:

video {
    width: 500px;
    background: blue;
}

***_ edit _***

*** _编辑_ ***

This is a better fiddle: http://jsfiddle.net/swaEe/40/

这是一个更好的小提琴:http://jsfiddle.net/swaEe/40/

The video playback should stay vertically and horizontally centered in the container. I want the "bars" to be transparent or the same color as the container (red in this case...).

视频播放应在容器中垂直和水平居中。我希望“条形”是透明的或与容器颜色相同(在这种情况下为红色......)。


6 个解决方案

#1


2  

How about div with css as background? [I'm not familiar with iOS, tasted on IE11]

用css作为背景的div怎么样? [我不熟悉iOS,在IE11上品尝过]

html:

CSS:

video {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
#container{
    width: 500px;
    height: 240px;
    background: blue;
}

jsfiddle: http://jsfiddle.net/c9aHf/1/

#2


2  

Solved! Link to the live jsbin: http://jsbin.com/AVOZoXu/9
The edit jsbin to follow the explanation: http://jsbin.com/AVOZoXu/9/edit
I couldn't test it on IE but it should work like a charm.

解决了!链接到现场jsbin:http://jsbin.com/AVOZoXu/9编辑jsbin按照说明:http://jsbin.com/AVOZoXu/9/edit我无法在IE上测试但它应该工作喜欢魅力。

There is a real problem with iOS. You won't be able to set a background color to the player and the default player size is 150x300 as you can see in Safari Developer Library:

iOS存在一个真正的问题。您将无法为播放器设置背景颜色,默认播放器大小为150x300,如Safari Developer Library中所示:

Because the native dimensions of a video are not known until the movie metadata loads, a default height and width of 150 x 300 is allocated on devices running iOS if the height or width is not specified. Currently, the default height and width do not change when the movie loads, [...]

由于在加载电影元数据之前不知道视频的原生尺寸,因此如果未指定高度或宽度,则在运行iOS的设备上分配默认高度和宽度150 x 300。目前,电影加载时默认的高度和宽度不会改变,[...]

So, what you have to do to remove the black bars is do change the default size and adapt it to the movie size as soon as you can. And yes, we'll need Javascript.

因此,要删除黑条,您需要做的是更改默认大小并尽快使其适应电影大小。是的,我们需要Javascript。

// set height and width to native values
function naturalSize() {
  var myVideo = document.getElementById('theVideo');
  var myCOntent= document.getElementById('content');
  myVideo.height = myVideo.videoHeight;
  myVideo.width = myVideo.videoWidth;

  //if the video is bigger than the container it'll fit
  ratio = myVideo.videoWidth/myVideo.videoHeight;
  if(parseInt(myContent.offsetWidth,10)

And now you'll get a video player size of the video as soon as the meta data loads. Since the video doesn't have its black bars anymore, I just had to center it as text.

现在,只要元数据加载,您就会获得视频的视频播放器大小。由于视频不再有黑条,我只需将其作为文本居中。

Oh! And you wanted it to be responsive? Check it out, it doesn't matter the width you set to the #content because naturalSize() checks the ratio and the container's width and sets a smaller height for the video than the original, preventing the black bars appearing in original video height with a smaller width.

哦!而你希望它具有响应能力?检查一下,你设置为#content的宽度并不重要,因为naturalSize()会检查比率和容器的宽度,并为视频设置一个比原始高度更小的高度,防止黑条出现在原始视频高度中宽度更小。

The width is controlled with the max-width:100%; property so there's no need to change it manually.

宽度由最大宽度控制:100%;属性所以不需要手动更改它。

#content{
  background:blue;
  width:50%;
  height:auto;
  text-align:center;
}
video {
  max-width:100%;
  vertical-align:top;
}

I know, I know, the video doesn't get resized till you have started playing it, but it's the closest you're gonna get on iOS to do what you want. Anyway, I think it's a great solution, I hope it helps you.

我知道,我知道,视频在你开始播放之前不会调整大小,但它是最接近iOS的,你可以做你想做的事情。无论如何,我认为这是一个很好的解决方案,我希望它对你有所帮助。

#3


2  

I think I've managed to come up with a solution:

我想我已经设法提出了一个解决方案:

The problem is that it seems to be impossible to style these letterboxes cross-browser. So the trick then would be not to have letterboxes, by scaling the video element to the aspect ratio of the video file.

问题是,似乎不可能跨浏览器设置这些信箱的样式。因此,通过将视频元素缩放到视频文件的宽高比,诀窍就是没有信箱。

This solution has two potential drawbacks:

该解决方案有两个潜在的缺点:

  1. it requires Javascript
  2. 它需要Javascript

  3. you need to know the dimensions of the video file and write them into data-attributes

    您需要知道视频文件的尺寸并将其写入数据属性

The reason for this is that the browser does not know the dimensions of the video file until it has started loading it. Most browsers do load a few bytes of the video before it is played, but not all - some older versions of Android wait until the user starts playing the video.

原因是浏览器在开始加载视频文件之前不知道视频文件的尺寸。大多数浏览器在播放前会加载几个字节的视频,但不是全部 - 一些旧版本的Android会等到用户开始播放视频。

If you do not care about Android 2.3, waiting for the loadedmetadata event to get videoWidth and videoHeight as jaicabs answer does it is the right way.

如果你不关心Android 2.3,那么等待loadedmetadata事件将videoWidth和videoHeight作为jaicabs的答案,这是正确的方法。

Take a look at this: run fiddle / fiddle editor

看看这个:运行小提琴/小提琴编辑器

We basically do three things:

我们基本上做了三件事:

  1. calculate the aspect ratio of the video
  2. 计算视频的宽高比

  3. resize it so that it fits snugly into its container
  4. 调整它的大小,使其紧贴其容器

  5. center it horizontally and vertically within the container
  6. 在容器内水平和垂直居中

You can now simply set the background-color of the container, and you're done.

您现在可以简单地设置容器的背景颜色,然后就完成了。

I've tested it with iOS 7 on iPhone and iPad, Chrome, Firefox and Safari. No IE testing so far, since I currently don't have my virtual machines handy, but I foresee no problems here for the current IEs.

我已经在iPhone和iPad,Chrome,Firefox和Safari上使用iOS 7进行了测试。到目前为止还没有IE测试,因为我目前没有方便的虚拟机,但我预见到当前的IE没有问题。

#4


0  

  1. remove the inline width/height attributes. You want to use CSS to control your layout!
  2. 删除内联宽度/高度属性。您想使用CSS来控制您的布局!

  3. Use the magic keyword 'auto' for your height
  4. 为你的身高使用魔术关键字'auto'

  5. Make sure to also use a poster with the same aspect ratio of your video
  6. 确保还使用与视频宽高比相同的海报

Here is a fiddle: http://jsfiddle.net/swaEe/13/

这是一个小提琴:http://jsfiddle.net/swaEe/13/

video {
    width: 500px;
    min-width: 200px;
    max-width: 100%;
    height: auto;
}

#5


0  

Solved with no JS:

解决没有JS:


Basically, in both cases removing the height or width setting for the video (for the thin div, I removed the height, and for the short one, the width). Then centered the video elements (horizontal with display:block and then the margin trick, vertical with display:table-cell, and there's probably a better way to do that one).

基本上,在两种情况下都删除了视频的高度或宽度设置(对于瘦div,我删除了高度,对于短的高度,宽度)。然后将视频元素居中(水平显示:块然后是边缘技巧,垂直显示:table-cell,这可能是更好的方法)。

#6


0  

I know some time has already passed since the question was asked, but I also had to implement a workaround for this problem and would like to share. The problem was similar to OP's, in that the video could be any size or aspect ratio.

我知道自提出问题以来已经过了一段时间,但我还必须为这个问题实施一个解决方法,并希望分享。问题类似于OP,因为视频可以是任何大小或宽高比。

If the HTML element is contained within a

which specifies no size at all, the container will automatically fit itself around the video and it will have no black "padding".

如果HTML

中,它根本没有指定大小,则容器将自动适应视频,并且没有黑色的“填充”。

Knowing this, we can take advantage of the loadedmetadata event: we don't actually need the video's dimensions for any calculations, but we must wait for this data to load so that the container will resize. As soon as that happens, we can adjust the container's position horizontally and/or vertically.

知道了这一点,我们可以利用loadedmetadata事件:我们实际上并不需要视频的维度来进行任何计算,但我们必须等待加载这些数据,以便容器调整大小。一旦发生这种情况,我们就可以水平和/或垂直调整容器的位置。

Here's a fiddle tested in IE11:

这是在IE11中测试的小提琴:

http://jsfiddle.net/j6Lnz31y/

Source (in case the fiddle ever becomes unavailable):

来源(如果小提琴变得不可用):



.whatever-container {
    position: relative;
}

.video-container {
    position: absolute;
    opacity: 0;
}

.video-container > video {
    position: relative;
    width: 100%;
    height: 100%;
}

/*
 * After the video dimentions have been acquired, its container will have
 * resized and we can adjust its position as necessary.
 */
function adjustVideoContainer() {
    console.log("video metadata loaded");
    var videoCOntainer= $(this).parent().filter(".video-container");

    if (videoContainer.length === 0) {
        //abort
        console.log(
            "adjustVideoContainer() was called but no it wasn't "+
            "wrapped in an appropriate container"
        );
        return;
    }
    var cOntainerParent= videoContainer.parent();
    var parentWidth     = containerParent.width(),
        parentHeight    = containerParent.height(),
        cOntainerWidth= videoContainer.width(),
        cOntainerHeight= videoContainer.height();

    if (containerWidth 

推荐阅读
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C++中省略号类型和参数个数不确定函数参数的使用方法,并提供了一个范例。通过宏定义的方式,可以方便地处理不定参数的情况。文章中给出了具体的代码实现,并对代码进行了解释和说明。这对于需要处理不定参数的情况的程序员来说,是一个很有用的参考资料。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
author-avatar
丿氵小柒柒2502894463
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有