Yerpasaur
Yerpasaur

Reputation: 71

Audio in phoneGap will not stop

I'm trying to make the play/stop the same element. The audio file plays perfectly but will not stop on second "click."

var audio = null;
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
    audio = new Media("/android_asset/www/song.mp3", 

            if (audio) {
                audio.stop();
            }

    );
}

$(document).ready(function(){
    $('#clickhere').click(function(event){ 
        audio.play();
    });
});

Upvotes: 1

Views: 191

Answers (2)

Neil Cresswell
Neil Cresswell

Reputation: 1165

Your existing stop only fires when the page is first loaded. It won't fire again when you press 'clickhere'. So, you need to add a toggle to the clickhere part to toggle playing on and off. Something like this:

var playing = false;

$('#clickhere').click(function(event)
{
    if (playing)
    {
        audio.stop();
        playing = false;
    }
    else
    {
        audio.play();
        playing = true;
    }
});

Upvotes: 1

SoulRayder
SoulRayder

Reputation: 5166

I think you are looking to do this:

  if (audio.isPlaying()) {
            audio.stop();
        }

Upvotes: 0

Related Questions