简体   繁体   English

改变HTML5视频元素源后不久寻求的问题

[英]Issues with seeking shortly after changing source of HTML5 video element

I'm attempting to use HTML5 to simulate a more robust multimedia player which loads ads in the background and swaps between video and ads as required. 我正在尝试使用HTML5来模拟更强大的多媒体播放器,该播放器在后台加载广告并根据需要在视频和广告之间进行切换。

The first challenge I ran into was that most mobile devices don't allow you to pre-load video, meaning the very concept of loading ads "in the background" isn't possible. 我遇到的第一个挑战是大多数移动设备不允许您预加载视频,这意味着无法在后台加载广告的概念。 Instead I took a much less elegant approach: have a single <video> tag and change its src attribute when it's time to throw an ad. 相反,我采用了一种不那么优雅的方法:拥有一个<video>标签,并在投放广告时更改其src属性。 The advantage to this is that you can commandeer the existing permission to pre-load and auto play, but the disadvantage is nasty buffering every time the video element changes. 这样做的好处是你可以使用现有的预加载和自动播放权限,但缺点是每次视频元素改变都会产生令人讨厌的缓冲。 This was acceptable to our clients, however. 但是,我们的客户可以接受这一点。

So the ideal behavior is: 所以理想的行为是:

  1. Content plays for 1 minute 内容播放1分钟
  2. Ad plays for 1 minute 广告播放1分钟
  3. Content resumes starting at 2 minutes 内容从2分钟开始恢复

The second challenge I ran into was getting content to resume at the correct location. 我遇到的第二个挑战是让内容在正确的位置恢复。 When you change the src attribute, the video starts playing from time 0. As a result I had to go with another ugly solution: Wait for the video to load ( canPlayThrough event) then perform a seek to the desired time. 当您更改src属性时,视频将从时间0开始播放。因此,我不得不使用另一个丑陋的解决方案:等待视频加载( canPlayThrough事件)然后执行搜索到所需的时间。 This actually causes it to buffer twice when switching from an ad back to content - but it's an acceptable cost for not using the native apps. 这实际上导致它在从广告切换回内容时缓冲两次 - 但是不使用本机应用程序是可接受的成本。

To figure out exactly where we should seek to, I have a "virtual content player" running in the background. 为了弄清楚我们应该寻找的确切位置,我在后台运行了一个“虚拟内容播放器”。 Basically a variable that stores the current time and updates when the <video> element throws an updateTime event. 基本上是一个变量,用于存储当前时间并在<video>元素抛出updateTime事件时进行更新。 So as the ad progresses, the virtual content advances at the exact same rate. 随着广告的进展,虚拟内容以完全相同的速率前进。 When it's time to switch back, we've skipped exactly one minute of content. 当需要切换回来时,我们已经跳过了一分钟的内容。

The challenge I've run into is that sometimes it seeks to a random location. 我遇到的挑战是, 有时它会寻找一个随机的位置。 I have absolutely no idea why. 我完全不知道为什么。 As an example, here's the console logs for different seek events when I played an ad at 7:39 (459 seconds) that was supposed to end at 8:09 (489 seconds): 例如,这是我在7:39(459秒)播放广告的不同搜索事件的控制台日志,该广告应该在8:09(489秒)结束:

@@ seeking from: 30.06833267211914, to: 0
@@ seeked to: 0
Content currentTime: 0
Content currentTime: 489.0016784667969
@@ seeking from: 489.0016784667969, to: 489.0016784667969
Content currentTime: 489.0016784667969
@@ seeking from: 489.0016784667969, to: 489.0016784667969
Content currentTime: 489.0016784667969
@@ seeked to: 489.0016784667969
Content currentTime: 489.0016784667969
Content currentTime: 441.502197265625
Content currentTime: 441.3605346679683
Content currentTime: 441.6107177734375

To explain what you're looking at: 解释你在看什么:

I have a seekFrom variable that is updated when the updateTime event fires. 我有一个在updateTime事件触发时更新的seekFrom变量。 This is used to get the "from" value during the seeking event. 这用于在seeking事件期间获取“from”值。 The to value is equal to video.currentTime . to值等于video.currentTime During the seeked event I just show video.currentTime . seeked事件中,我只是显示video.currentTime

So originally there was a seek from 30 to 0 (this is the "seek to 0" performed when the source is changed). 所以最初有一个从30到0的搜索(这是在更改源时执行的“寻找0”)。 Shortly afterwards the current time changed to 489 before the seek event fired. 不久之后,当前时间在搜索事件被触发之前变为489。 There were then two seek events -- one representing the change from 0 to 489 (which was misinterpreted as 489 to 489 since the current time updated early) and another representing an unsolicited change from 489 to 441 (also misinterpreted as 489 to 489 since the current time updated late ). 然后有两个寻找事件 - 一个表示从0变为489(自当前时间更新以来被误解为489到489),另一个表示从489到441的未经请求的变化(也被误解为489到489)当前时间更新晚了 )。 After this the current time changed to 441. 在此之后,当前时间变为441。

All of this was created by the following two lines of code: 所有这些都是由以下两行代码创建的:

video.src = content.source;
video.currentTime = content.currentTime;

At the time those two lines were run, content.currentTime was equal to 489. Nowhere else do I set video.currentTime manually, and no seeks were performed on the seek bar. 在运行video.currentTimecontent.currentTime等于489.我没有手动设置video.currentTime ,也没有在搜索栏上执行搜索。 The number 441 seemed to come out of thin air, but if I replay the video over and over again it always seeks to this exact same location -- so there must be some significance. 数字441似乎是凭空出现的,但如果我一遍又一遍地重播视频,它总是寻求这个完全相同的位置 - 所以必须有一些意义。

So the question is: 所以问题是:

Why on Earth is it seeking twice, and how do I stop it? 为什么在地球上它寻求两次,我该如何阻止它?

Note: This behavior was all experienced using QuickTime on a Macbook Pro. 注意:在Macbook Pro上使用QuickTime都会遇到这种情况。 I haven't even gotten to testing on iOS yet (since it isn't working right) and Android will be a whole different issue all together when I get there. 我还没有在iOS上进行测试(因为它不能正常工作),当我到达那里时,Android将是一个完全不同的问题。

Also, note: I receive a seeking event prior to the second seek - so if there's any way in HTML5 to "cancel" a seek, that might be an acceptable solution. 另外,请注意:我在第二次搜索之前收到了一个seeking事件 - 所以如果HTML5中有任何方法可以“取消”搜索,那么这可能是一个可接受的解决方案。

Not sure why someone gave a down vote... I don't see what's illegitimate about my question. 不知道为什么有人投了一票...我不明白我的问题是不合法的。 After experimenting and researching and trying every random suggestion I could find on obscure blog posts I found something that works: 经过实验,研究和尝试每一个随机的建议,我可以在晦涩的博客文章中找到我找到的东西:

iOS has trouble seeking to decimal locations . iOS无法寻找小数位 A seek to 491.337 will fail, but a seek to 491 will succeed. 寻求491.337将失败,但寻求491将成功。

I just added the following: 我刚刚添加了以下内容:

video.currentTime = content.currentTime | 0; // Shortcut for truncating positive integers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM