javascript - 设置 HTML5 音频位置

标签 javascript google-chrome html5-audio

如何跳转到 HTML5 Audio 元素中的特定时间偏移量?

他们说你可以简单地设置他们的 currentTime property (强调我的):

The currentTime attribute must, on getting, return the current playback position, expressed in seconds. On setting, if the media element has a current media controller, then it must throw an INVALID_STATE_ERR exception; otherwise, the user agent must seek to the new value (which might raise an exception).

唉,它似乎不起作用(我在 Chrome 中需要它)。

有类似的问题,虽然没有答案。

最佳答案

要跳转音频文件,您的服务器必须正确配置。

客户端发送byte range requests寻找和播放文件的某些区域,因此服务器必须充分响应:

In order to support seeking and playing back regions of the media that aren't yet downloaded, Gecko uses HTTP 1.1 byte-range requests to retrieve the media from the seek target position. In addition, if you don't serve X-Content-Duration headers, Gecko uses byte-range requests to seek to the end of the media (assuming you serve the Content-Length header) in order to determine the duration of the media.

然后,如果服务器正确响应字节范围请求,您可以通过currentTime设置音频的位置:

audio.currentTime = 30;

参见 MDN 的 Configuring servers for Ogg media (实际上,这同样适用于其他格式)。

另请参阅 Configuring web servers for HTML5 Ogg video and audio .

关于javascript - 设置 HTML5 音频位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9563887/

相关文章:

google-chrome - 什么是 manifest.json 文件,我在哪里可以在我的机器上找到它?

google-chrome - 是否可以打开 Chrome 开发人员工具选项卡的多个实例或 Split View?

javascript - 将javascript与计算机声音输入设备集成

javascript - <audio> 的正确加载

android - HTML5 音频 blob 无法在 Android 上播放

javascript - 如何在 onclick 方法后更改 div 的 CSS 样式

javascript - 匹配两个日期,如果匹配则返回 true

c# - 使用 JavaScript 为 MVC 应用构建相对 URL

javascript - 如何使用 TypeScript 在浏览器中使用 NPM 包?

google-chrome - 如果也在 Windows 7+10 上本地安装,Google 网络字体 "Open Sans"不会以正确的粗细显示