- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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.
- Tags:
- HTML5
- Intel® XDK
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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. } }
Link kopiert
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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. } }
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
What a concise solution! Works like a charm. Thanks a lot, Brandon.

- RSS-Feed abonnieren
- Thema als neu kennzeichnen
- Thema als gelesen kennzeichnen
- Diesen Thema für aktuellen Benutzer floaten
- Lesezeichen
- Abonnieren
- Drucker-Anzeigeseite