[英]play audio on jsp file in my project
I am making a game and want to play audio from my jsp page. 我正在做一个游戏,想要从jsp页面播放音频。 I have tried to do this using various solutions but none of them were helpful to me.
我尝试使用各种解决方案来实现此目的,但是没有一个对我有帮助。
I tried this way and some others. 我以这种方式尝试了一些。 What did I not try yet?
我还没有尝试什么?
playSound('game_start_sound'); function playSound(id){ var audio = document.getElementById(id); audio.play(); // alert("played"); }
<audio id="game_start_sound"> <source src="http://localhost:8080/firstproject/sound/gamestart.mp3" type='audio/mpeg; codecs="mp3"'></embed> </audio> <audio id="game_start_sound"> <embed src="<%=request.getContextPath()%>/sound/gamestart.mp3" type='audio/mpeg; codecs="mp3"'></embed> </audio> <audio id="game_start_sound"> <embed src="http://localhost:8080/firstproject/sound/gamestart.mp3" type='audio/mpeg; codecs="mp3"'></embed> </audio> <embed id="game_start_sound"> <source src="http://localhost:8080/firstproject/sound/gamestart.mp3" type='audio/mpeg; codecs="mp3"'></embed> </embed>
Why using <embed>
? 为什么使用
<embed>
? Use <source>
instead 使用
<source>
代替
<audio id="game_start_sound">
<source src="http://localhost:8080/firstproject/sound/gamestart.mp3" type="audio/mpeg"></source>
<source src="http://localhost:8080/firstproject/sound/gamestart.ogg" type="audio/ogg"></source>
</audio>
For firefox you need to add ogg files.Find here full chart of audio type supported by each browser 对于Firefox,您需要添加ogg文件。 在此处查找每个浏览器支持的音频类型的完整图表
You seem to have the wrong ID. 您似乎输入了错误的ID。 Try this:
尝试这个:
playSound('game_start_sound');
function playSound(id){
var audio = document.getElementById(id);
audio.play();
}
<audio id="game_start_sound">
<source src="http://localhost:8080/firstproject/sound/gamestart.mp3" type='audio/mpeg; codecs="mp3"'></source>
</audio>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.