停止缓冲 HTML5 视频,但保留缓冲数据。然后恢复

Stop buffering HTML5 video, but preserve buffered data. And then resume

我正在做一个可以在两个或多个视频之间切换的项目。

所以如果 Video-A 正在播放并且我切换到 Video-B 我希望能够停止缓冲 Video-A,开始缓冲并播放Video-B。然后,如果你想返回 Video-B,请停止 B 并继续 A,而无需再次开始缓冲。

我想防止 App 一次缓冲多个视频以节省带宽。

到目前为止,我找到的唯一解决办法是删除视频源并重新放置。

video.src = ""

我不喜欢这个解决方案。我很想在视频 API 中提供此内容,例如:

video.stopBuffering()
vide.resumeBuffering()

但好像并不是这样。有任何想法吗?

简答:做不到。

没有 API 公开方法来执行此操作,几乎完全取决于浏览器如何处理实际的 loading/buffering 策略(有一些已定义的步骤,但策略不是).

standard:

中定义的一些内部结构

The rate of the download may [also] be throttled automatically by the user agent, e.g. to balance the download with other connections sharing the same bandwidth.

User agents may decide to not download more content at any time, e.g. after buffering five minutes of a one hour media resource, while waiting for the user to decide whether to play the resource or not, while waiting for user input in an interactive resource [...]

The user agent may use whatever means necessary to fetch the resource (within the constraints put forward by this and other specifications); for example, reconnecting to the server in the face of network errors, using HTTP range retrieval requests, or switching to a streaming protocol. The user agent must consider a resource erroneous only if it has given up trying to fetch it.

您可以随时暂停视频。这可能会使浏览器在几秒钟后暂停下载数据,但同样,这取决于浏览器如何实际处理。

所以,总而言之:不管我们喜不喜欢,它就是这样。

媒体 preload 属性将有助于探索,尽管它仅适用于 in some browsers

HTML5 规范似乎预料到了您的确切用例: http://www.w3.org/TR/html5/embedded-content-0.html#attr-media-preload

Note: Authors might switch the attribute from "none" or "metadata" to "auto" dynamically once the user begins playback. For example, on a page with many videos this might be used to indicate that the many videos are not to be downloaded unless requested, but that once one is requested it is to be downloaded aggressively.