Reputation: 551
When the function generate is called, the sound should be played. If button is triggered sound sound should be stopped. What's the best way to do this?
<script>
var audio = new Audio('assets/spin.mp3');
function generate()
{
/* some code goes here*/
audio.play();
}
</script>
<button onClick="game.rotationAngle=0; game.draw();generate();audio.stop();">start</button>
It's a kind of reset I need to perform when the button is triggered. That's why the sound needs to be stopped, before it will run again.
Upvotes: 0
Views: 3896
Reputation: 19024
audio
isn't valid outside the <script>
tag, since you define it in the script tags.Below is the resulting code.
<script>
var audio = new Audio('assets/spin.mp3');
function generate() {
/* some code goes here*/
if(audio.paused) {audio.currentTime=0;audio.play()}
else audio.pause();
}
</script>
<button onClick="game.rotationAngle=0; game.draw();generate();">start</button>
Upvotes: 3
Reputation: 11313
There is no such function as audio.stop()
.
You can use the following instead:
function generate() {
if(!audio.paused) { /* Check if it's not paused */
audio.pause(); /* To pause the audio */
audio.currentTime = 0; /* To reset the time back to 0 */
}
else {
audio.play(); /* To make it play again */
}
};
Upvotes: 0
Reputation: 1461
You might want to use audio.pause()
instead since there is no stop
method. If you check Audio.prototype.stop
it's undefined.
Upvotes: 2