Software Archive
Read-only legacy content
17061 Discussions

looping background sound using cordova media plugin won't stop when exit app

Bigfanx__
Beginner
964 Views

Hi, I was playing cordova media plugin in intel xdk. I managed to make the background music to loop in my app using the following code because cordova doesn't support media loop directly. The sound indeed loops on my android phone but not on the emulator and there is also an obvious gap between each play, but I can live with it at the moment :-)

......
bgSound = new Media(media,  mediaSuccess, mediaError, mediaLoop) ;
......
function mediaLoop(status) {
		if(status === Media.MEDIA_STOPPED){
			bgSound.play();
		}
    }

I also added a function for users to confirm if they want to exit the app or not when they press the back button on the phone.

function onLoad(){
	document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady(){
	document.addEventListener("backbutton", onBackKeyDown, false);
}

function onBackKeyDown() {
	navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}

function onConfirm(button) {
    if(button == 2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

However, when I open the app, play it for a while and then exit the app by pressing the back button on the phone, the background music, which I assume it should stop as well, is still playing. If reopen the app, I finally have double background music playing:-(  . I guess it is the code for looping music causes the trouble. How to stop the looping background music when I press the back button? Thanks a lot in advance.

0 Kudos
1 Solution
Brandon_K_Intel
Employee
964 Views

You need to add logic to stop the running media on your device when you press the back button. The way you have it set up all status changes in the mediaLoop callback and restart the music when it stops. This is fine when the app is running in the background but will cause your media to keep running.

Assuming you have reference to bg_media in your onConfirm, you could do the following:

This can probably be improved upon but it gives you a place to start.

var loopMediaflag; // flag to prevent restart of media on state change

bgSound = new Media(media,  mediaSuccess, mediaError, mediaLoop) ;

function mediaLoop(status) {
        if(status === Media.MEDIA_STOPPED){
        	if(loopMediaflag === true)
        	{
            	bgSound.play();
            }
        }
}

function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
    loopMediaflag = true;
}

function onConfirm(button) {

    if(button == 2){//If User selected No, then we just do nothing
        return;
    }else{
    	loopMediaflag = false
    	if (bgSound) {
                bgSound.stop()
            }
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

 

View solution in original post

0 Kudos
2 Replies
Brandon_K_Intel
Employee
965 Views

You need to add logic to stop the running media on your device when you press the back button. The way you have it set up all status changes in the mediaLoop callback and restart the music when it stops. This is fine when the app is running in the background but will cause your media to keep running.

Assuming you have reference to bg_media in your onConfirm, you could do the following:

This can probably be improved upon but it gives you a place to start.

var loopMediaflag; // flag to prevent restart of media on state change

bgSound = new Media(media,  mediaSuccess, mediaError, mediaLoop) ;

function mediaLoop(status) {
        if(status === Media.MEDIA_STOPPED){
        	if(loopMediaflag === true)
        	{
            	bgSound.play();
            }
        }
}

function onDeviceReady(){
    document.addEventListener("backbutton", onBackKeyDown, false);
    loopMediaflag = true;
}

function onConfirm(button) {

    if(button == 2){//If User selected No, then we just do nothing
        return;
    }else{
    	loopMediaflag = false
    	if (bgSound) {
                bgSound.stop()
            }
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

 

0 Kudos
Bigfanx__
Beginner
964 Views

What a concise solution! Works like a charm. Thanks a lot, Brandon.

0 Kudos
Reply