簡體   English   中英

如何播放使用 accept 導入的音頻文件?

[英]How do I play a audio file that is imported with accept?

敵人的例子我有這個代碼

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

我想使用 javascript 播放此音頻。 我能怎么做?

您可以通過以下兩種方式之一執行此操作。 由於您要求使用 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM