iPad 上的 HTML5 视频
我有一个动态视频库,它在计算机上运行得很好。转移到 iPad 时,视频开始加载并显示无法播放图标。相反,我宁愿视频在准备好播放之前不显示。我尝试添加“canplaythrough”和“canplay”的事件监听器,以及当它们发生时视频淡入然后播放。 iPad不支持这些事件吗?
new_video = document.createElement('video');
new_video.setAttribute('class', 'none');
new_video.setAttribute('width', '568');
new_video.setAttribute('height', '269');
new_video.setAttribute('id', 'video'+video_num);
current_video.insertBefore(new_video, video_controls);
new_video.load();
new_video.addEventListener('canplaythrough', function() {
$('#video'+video_num').fadeIn(100);
new_video.play();
});
I have a dynamic video gallery and it works great on a computer. When moving to an iPad, the video starts loading and it shows the cannot play icon. Instead of this I'd rather the video not show until it's ready to play. I have tried to add events listeners for "canplaythrough" and "canplay" and when they occur for the video to fade in then play. Does the iPad not support these events?
new_video = document.createElement('video');
new_video.setAttribute('class', 'none');
new_video.setAttribute('width', '568');
new_video.setAttribute('height', '269');
new_video.setAttribute('id', 'video'+video_num);
current_video.insertBefore(new_video, video_controls);
new_video.load();
new_video.addEventListener('canplaythrough', function() {
$('#video'+video_num').fadeIn(100);
new_video.play();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
解决这个视觉问题的方法是隐藏视频元素,直到准备好播放为止。请注意,您无法设置
display:none
(如果您这样做,视频将不会加载),并且visibility:hidden
也无法解决该问题。要修复此问题,请使用
overflow:hidden
将视频元素包装在 DIV 上,并设置-webkit-transform:translateX(1024px)
(一个足够高的数字,以便将视频推到外面屏幕)。您必须监听
canplay
或canplaythrough
或load
事件(根据您的需要)并设置translateX
之后为零。The way to solve this visual problem is to hide the video element until it is ready to be played. Note that you cannot set
display:none
(video won't load if you do that) andvisibility:hidden
won't solve the issue either.To fix it, wrap the video element on a DIV with
overflow:hidden
and set-webkit-transform:translateX(1024px)
(a number high enough to push the video outside the screen).Than you have to listen for the
canplay
orcanplaythrough
orload
events (based on your need) and set thetranslateX
to zero after that.在 iPad 上,只有用户开始活动时,它才会加载视频,这是苹果公司的设计,旨在防止 iPhone 或 iPad 用户耗尽他们的数据计划。所以你将无法做你想做的事,抱歉。
查看此链接并查看有关特定于设备的注意事项部分的一些信息。但它不会开始加载数据,因此在用户触摸它之前它无法触发 canplaythrough 事件。
On the iPad it will not load the video until a user starts an event this is by design of apple to prevent iPhone or iPad users from burning up their data plans. So you will not be able to do what you want sorry.
Check out this link and look for the Device-Specific Considerations section for some info. But it will not start loading data so it couldn't fire the canplaythrough event until a user touches it.
问题似乎是动态创建视频对象 - 这以某种方式破坏了 iPad 上的代码。
我编写了一个视频播放器,将已存在的视频元素移动到容器中,然后视频立即停止工作。不过在其他系统上没问题...
我猜你必须找到一种方法来让视频元素就位,然后在其上方和下方添加所有其他代码......
the problem seems to be dynamically creating the video object - that somehow breaks the code on the iPad.
I wrote a video player that moves the alreay present video element into an container and immediately the video stops working. no problem on other systems though...
guess you gotta find a way to have the video element already in place and then add all your other code above and below it....
在尝试加载视频之前检查浏览器是否可以播放视频:
摘自深入了解 HTML5 附录 A。
Check to see if the browser can play the video before you attempt to load it:
Taken from Dive Into HTML5 Appendix A.
在 ipad/iphone 上自动启动播放器:
automatically starts player on the ipad/iphone:
意识到这是一个老问题,但如果其他人偶然发现这个问题,我也遇到了类似的问题。
查看视频元素调度的事件,iPad 似乎将开始加载视频,然后几乎立即暂停它(似乎与第一个“进度”事件同时发生)。
在播放实际开始之前,它不会触发“canplay”或“canplaythrough”事件,因此您必须监听在播放开始之前触发的 3 个事件之一:
我会将您的处理程序更改为“loadstart”并尝试一下。
Realize this is an old issue, but if anyone else stumbles across this, I encountered a similar problem.
Looking at the events dispatched by the video element, it looks like the iPad will start loading the video, then almost immediately suspend it (appears to be simultaneous with the first 'progress' event).
It will not fire the "canplay" or "canplaythrough" events until after playback has actually begun, so you must listen to one of the 3 events that do fire before playback begins:
I would change your handler to 'loadstart' and give that a shot.
这是熟悉 HTML5 事件的好地方。
http://www.w3.org/2010/05/video/mediaevents.html
Here's a GREAT place to get familiar with HTML5 events.
http://www.w3.org/2010/05/video/mediaevents.html
这可能就是为什么您的 MP4 视频在完全加载之前无法播放的原因: 如何让 HTML5 MP4 视频在完全加载之前播放。无论如何值得一试。
This is possibly why your MP4 video won't play until it's fully loaded: how to get your HTML5 MP4 video to play before being fully loaded. Worth a shot anyway.