以 HTML 格式播放音频
我想播放 HTML 格式的 mp3 音频文件。我不想使用音量等控件显示整个播放器。 我只需要一个播放/暂停按钮,由我设计,而不是某些播放器的核心设计,例如雅虎播放器或谷歌播放器。
例如,音频将自动播放。单击按钮(可能是图像)时,它将暂停,再次单击该图像时,音频将再次播放。
这里有很多例子: http://www.w3schools.com/html/html_sounds.asp< /a>
我可以通过 JS 代码控制它们中的任何一个播放/停止吗?
I want to play a mp3 audio file in HTML. I don't want to display the whole player with controls like Volume etc.
I just need a play/pause button, Designed by me, not the core design of some player, like yahoo player, or google one.
For example the audio will be autoplay. When a button (probably an image) is clicked it will pause, and when that image is clicked again, the audio will play again.
There are quite few examples here : http://www.w3schools.com/html/html_sounds.asp
Can I control any of them to play/stop from JS code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 html5 音频标签。您可以指定自己的播放控件。
这是一个 jQuery 解决方案。
http://jsfiddle.net/QPW27/109/
这就是您的非 jQuery 解决方案的外观喜欢。
不同的浏览器支持不同的音频格式。不过,您可以指定后备音频版本。该网站有一个很好的浏览器支持图表 截至 2011 年 7 月。
You can use the html5 audio tag. You can specify your own controls for playback.
This is a jQuery solution.
http://jsfiddle.net/QPW27/109/
This is what your non-jQuery solution would look like.
Different browsers support different audio formats. You can specify fallback audio versions though. This website has a nice chart of browser support as of July 2011.
希望几年后,HTML5 音频 API将得到更多浏览器的支持,但目前,播放声音需要大量特定于浏览器的技巧才能正常工作,或者依赖 Flash 等浏览器插件。
同时,我建议使用 SoundManager2。它相当容易使用,并且比您自己操作要少得多。
Hopefully, in a few years, the HTML5 audio API will be supported accross more browsers, but currently, playing sounds requires either a lot of browser-specific hacks to get things working, or reliance on a browser plugin like flash.
In the meantime, I reccomend using SoundManager2. It's fairly easy to work with and will involve much less headache than doing it yourself.
Audio.js 看起来它具有您正在寻找的播放器样式功能,并优雅地降级为如果浏览器不支持新的音频 API,则使用 Flash。
Audio.js looks like it has the player styling features you're looking for, with a graceful degradation to Flash if the browser doesn't support the new audio API.
您可以使用嵌入标签来播放音频
You can play audio by using embed tag