简体   繁体   English

使用HTML,JS中的按钮切换音频播放

[英]Toggle audio play back with button in HTML, JS

I would like to have a button that will play an audio file. 我想要一个可以播放音频文件的按钮。 The icon of the button will change if the file is playing or stopped. 如果文件正在播放或停止,则按钮的图标将更改。 This is what the code for the button looks like 这是按钮的代码如下所示

play_circle_filled play_circle_filled

If the JS could use getellimentbyID to change the button. JS是否可以使用getellimentbyID更改按钮。 The bit I can seem to do is the toggling for play to stopped. 我似乎可以做的就是停止播放。 All help will be much appreciated. 所有帮助将不胜感激。 Thanks James 谢谢詹姆斯

This is a basic code to play audio in html5 这是在html5中播放音频的基本代码

<audio id="player">
  <source src="myfile.mp3" />
</audio>

Then in your script, you can run this when the button is pressed: 然后在脚本中,可以在按下按钮时运行此命令:

document.getElementById('player').play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM