ZaraaKai
ZaraaKai

Reputation: 85

Javascript Audio object play/pause

I have theres function 1 is for playing music and the second is for set pause to the actual music, my pause function don't work. How can i do for set pause ?

function play(id){
        var audio = new Audio('music/'+id+'.mp3');
        audio.play();
}

function pause(){
        audio.pause();
}

Upvotes: 0

Views: 664

Answers (1)

Starfish
Starfish

Reputation: 3574

The audio variable isn't accessible in the Pause function. You need to pass it as a parameter or set it outside the function. This is just an example of how you can do it:

var audio;

function createAudio(id) {
    audio = new Audio('music/'+id+'.mp3');
    play();
}

function play(){
    audio.play();
}

function pause(){
    audio.pause();
}

Upvotes: 2

Related Questions