我有一个游戏,我需要播放背景音乐,当我点击一个按钮时,它应该播放声音。它播放声音但停止音乐。
重新打开音乐的唯一方法是进行另一个事件,例如点击按钮。在背景音乐继续播放时有没有办法播放声音?
到目前为止,这是我的代码。
var a = new Audio("Latin Groove Hype App.mp3");
function init()
{
playBackGroundLoop();
}// in html(<body onload = "init()">)
function playSoundEffect()
{
var playSoundCorrect = new Audio("Correct synth.mp3");
playSoundCorrect.play();
}
function playBackGroundLoop()
{
a.addEventListener('ended', function()
{
this.currentTime = 0;
this.play();//plays music on loop
}, true);
a.play();
}
$(function()
{
$("#playButton").on("click",function()
{
playSoundEffect();
}
});
谢谢
答案 0 :(得分:0)
花点时间传递我的脚本以适应您的项目:
<强> HTML:强>
<ul id=nav>
<li><a href="javascript:;">Item 1</a></li>
<li><a href="javascript:;">Item 2</a></li>
</ul>
<audio id="audio-nav">
<source src="click.mp3"></source>
<source src="click.ogg"></source>
Your browser isn't invited for super fun audio time.
</audio>
<audio id="audio-bg">
<source src="bg.mp3"></source>
<source src="bg.ogg"></source>
Your browser isn't invited for super fun audio time.
</audio>
<a id="toggle" href="javascript:;">PLAY/PAUSE</a>
<强> JS:强>
var bgMusic = $('#audio-bg')[0],
navMusic = $('#audio-nav')[0],
playing = true;
bgMusic.addEventListener('ended', function() {
this.currentTime = 0;
if (playing) {
this.play();
}
}, false);
bgMusic.play();
$('#toggle').click(function() {
if (!bgMusic.paused) {
bgMusic.pause();
playing = false;
} else {
bgMusic.play();
playing = true;
}
});
$('#nav a').click(function() {
navMusic.play();
});
......在这里,完全functional demo