【问题标题】:How do I play a audio file that is imported with accept?如何播放使用接受导入的音频文件?
【发布时间】:2020-07-06 10:47:00
【问题描述】:

我有这个代码的例子

<input type="file" id="thefile1" accept="audio/*" />

我想使用 javascript 播放此音频。我该怎么办?

【问题讨论】:

标签: javascript html button audio var


【解决方案1】:

您可以通过以下两种方式之一执行此操作。由于您要求使用 JavaScript,因此您只需将 autoplay 设置为 true。第二种方法是做同样的事情,但在 video 元素本身上设置 autoplay="true"

在你的情况下,如果你不想自动播放,你只需要在你的视频元素上运行.play()

只是为了播放视频

document.getElementById('playButton').addEventListener('click', (e) => {
  const video = document.getElementsByTagName('video')[0];
  video.play();
});
<video width="320" height="240" controls>
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

<button id="playButton">Play</button>

自动播放选项

JavaScript 选项

const video = document.getElementsByTagName('video')[0];
video.autoplay = true;
<video width="320" height="240" controls>
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

HTML 选项

<video width="320" height="240" controls autoplay="true">
  <source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
  Your browser does not support the video tag.
</video>

【讨论】:

    猜你喜欢
    • 2012-07-31
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多