HTML 自动播放歌曲和 StartAt

标签 html html5-audio

我有这段代码,但我不知道如何让歌曲从 0:19 秒开始。你能帮我一下吗?

<div class="fin-subheading">
  &middot; ROLEPLAY &middot;
  <audio id='music' volume='0.5' autoplay controls>
    <source src="anonymous.mp3" type="audio/mpeg">
  </audio>
</div>

<script>
  var audio = document.getElementById("music");
  audio.volume = 0.3;
</script>

最佳答案

您可以在 src 属性本身中指定播放范围。请参阅文档 here :

When specifying the URI of media for an or element, you can optionally include additional information to specify the portion of the media to play. To do this, append a hash mark ("#") followed by the media fragment description.

A time range is specified using the syntax:

#t=[starttime][,endtime]

所以代替:

<source src="anonymous.mp3" type="audio/mpeg">

简单地说:

<source src="anonymous.mp3#t=n,m" type="audio/mpeg">

其中 nm 分别是开始时间和结束时间。

范围也可以是无界的。例如,您可以这样做:

<source src="anonymous.mp3#t=19" type="audio/mpeg">

从 19 秒开始一直播放到最后;甚至是这个:

<source src="anonymous.mp3#t=,19" type="audio/mpeg">

将从头开始持续 19 秒。

关于HTML 自动播放歌曲和 StartAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65640018/

相关文章:

javascript - 如何查找 SoundCloud 的轨道信息和 client_id

java - 在java中将HTML转换为RTF?

javascript - 如何使喜欢后出现的Facebook评论框显示在上方而不是下方

javascript - 如何在 canplay 事件中设置 HTMLMediaElement 的 .currentTime,调用播放并避免调度暂停事件?

javascript - 可以使用 Web Audio API 和 createMediaElementSource 分析来自 Icecast 的流音频吗?

javascript - 请求没有音频/视频捕获的完整 MediaDeviceInfo

javascript - HTML标签<audio>在不同浏览器中的表现

html - 当一个列变大时不要缩放其他列

javascript - 阻止禁用元素更改页面大小?

javascript - CodeMirror 不显示 HTML 模式