Having trouble controlling the audio with this code. When I click a button that has an onclick
calling playpause()
, it pauses the audio. But when I click again, it doesn't resume.
var audio=new Audio("music.mp3");
function music() {
audio.play();
audio.loop="true";
}
function playpause() {
if(audio.play)
{
audio.pause();
}
else if(audio.pause)
{
audio.play();
}
}
On the other hand, using the following code allows for both pause and play functions to be accessed from the same button:
var audio=new Audio("music.mp3");
function music() {
audio.play();
audio.loop="true";
}
var count=0;
function playpause() {
if(count%2==0)
{
audio.pause();
}
else
{
audio.play();
}
count++;
}
What's causing the issue in the first code snippet?