Javascript 在IPad上播放HTML5视频并搜索
很奇怪的虫子,我好像搞不懂 当用户点击play时,我试图从某个位置播放HTML5视频。我正试图让它在视频开始播放时正确搜索 在我的游戏事件中,我执行Javascript 在IPad上播放HTML5视频并搜索,javascript,ios,ipad,html,video,Javascript,Ios,Ipad,Html,Video,很奇怪的虫子,我好像搞不懂 当用户点击play时,我试图从某个位置播放HTML5视频。我正试图让它在视频开始播放时正确搜索 在我的游戏事件中,我执行this.currentTime=X 在浏览器上,它可以正常工作。但在IPad上,当我播放视频时,视频没有找到正确的位置(从零开始) 更奇怪的是,如果我在setTimeout中调用this.currentTime=X,比方说1秒,它在IPad上工作(有时)。尝试将X限制为1位小数 X.toFixed(1); 正如你所提到的,它有时会在1秒后工作。您
this.currentTime=X
在浏览器上,它可以正常工作。但在IPad上,当我播放视频时,视频没有找到正确的位置(从零开始)
更奇怪的是,如果我在setTimeout中调用this.currentTime=X,比方说1秒,它在IPad上工作(有时)。尝试将
X
限制为1位小数
X.toFixed(1);
正如你所提到的,它有时会在1秒后工作。您是否尝试在播放
事件触发后设置位置?或者甚至可以通过事件播放
查看的源代码,查看可在iOS、视频加载(请参阅第2项)而非页面加载时使用的事件的完整列表(在javascript文件中)。我的猜测是,当您运行this.currentTime=X
时,视频没有加载,因此没有效果。这也解释了为什么延迟操作有时可以解决问题:有时在一秒钟后加载,有时不加载
我没有要测试的iOS设备,但我建议将侦听器绑定到视频,以便您的currentTime
操作仅在视频开始加载后进行:
// within the play event handler...
if(!isIOSDevice) {
this.currentTime = X;
} else {
function trackTo(evt) {
evt.target.currentTime = X;
evt.target.removeEventListener("loadeddata", trackTo)
});
this.addEventListener("loadeddata", trackTo);
}
您需要根据当前访问是否来自iOS设备,在代码的其他地方设置iOSDevice
。感谢您尝试以下答案。不幸的是,如果当前时间大于0且小于1,则必须求助于仅检查内部时间更新,如果它随后转到视频的该部分并删除了时间更新的侦听器。虽然这个问题很老,但问题仍然存在。我发现了一个解决方案,可以在多台经过测试的iPad(1+2+3)上运行iOS 5和iOS 6(iOS 3+4未经测试):
基本上,您首先必须等待初始的播放
事件,然后为canplaythrough
添加一个一次性活页夹,然后为进度
——只有这样,您才能真正更改当前时间
值。在此之前的任何尝试都将失败
视频必须首先开始播放,这使得视频元素顶部的黑色层相当方便。不幸的是,视频中的声音无法通过JavaScript-->停用,这不是一个完美的用户体验
// https://github.com/JoernBerkefeld/iOSvideoSeekOnLoad / MIT License
// requires jQuery 1.8+
// seekToInitially (float) : video-time in seconds
function loadingSeek(seekToInitially, callback) {
if("undefined"==typeof callback) {
callback = function() {};
}
var video = $("video"),
video0 = video[0],
isiOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/) !== null,
test;
if(isiOS) { // get the iOS Version
test =navigator.userAgent.match("OS ([0-9]{1})_([0-9]{1})");
// you could add a loading spinner and a black layer onPlay HERE to hide the video as it starts at 0:00 before seeking
// don't add it before or ppl will not click on the play button, thinking the player still needs to load
}
video.one("playing",function() {
if(seekToInitially > 0) {
//log("seekToInitially: "+seekToInitially);
if(isiOS) {
// iOS devices fire an error if currentTime is set before the video started playing
// this will only set the time on the first timeupdate after canplaythrough... everything else fails
video.one("canplaythrough",function() {
video.one("progress",function() {
video0.currentTime = seekToInitially;
video.one("seeked",function() {
// hide the loading spinner and the black layer HERE if you added one before
// optionally execute a callback function once seeking is done
callback();
});
});
});
} else {
// seek directly after play was executed for all other devices
video0.currentTime = seekToInitially;
// optionally execute a callback function once seeking is done
callback();
}
} else {
// seek not necessary
// optionally execute a callback function once seeking is done
callback();
}
});
}
整个东西都可以从下载,这是正确的。一旦视频开始播放,Quicktime播放器将出现,在触发第一个“进度”事件之前,视频将无法查看。如果在此之前尝试查找,则会出现无效状态错误。这是我的密码:
cueVideo = function (video, pos) {
try {
video.currentTime = pos;
// Mobile Safari's quicktime player will error if this doesn't work.
} catch(error) {
if (error.code === 11) { // Invalid State Error
// once 'progress' happens, the video will be seekable.
$(video).one('progress', cueVideo.bind(this, video, pos));
}
}
}
哇!它确实起了作用。最终使用了爆米花库(对于同样使用爆米花js的用户,请收听loadeddata事件)@K2xL很棒,很高兴您修复了它!我更新了答案,按照您的建议使用loadeddata
。(事实证明,loadeddata
是一个标准的HTML5事件,而不仅仅是一个定制的爆米花事件。)好吧,我真的不想说@apsillers,但实际上它不起作用。我想是的(我实际上是在轨道的第一行发出警报,以确保事件在ipad上启动。当我点击“OK”时视频可能处于缓冲状态,因此它工作正常。我将继续尝试您的各种解决方案。将让您知道您可能会尝试上列出的其他一些事件。可以播放
和时间更新
似乎是可能的选项。