Play sound even when app is in background?

Is there a possibility to left a music playing even if the app is running in background? I don’t want it to be a feature request because I don’t find it useful for games, but I’m thinking about an app that plays a white noise for my children constantly, even if the phone screen is off after a moment. So maybe any of you, guys, have any idea? :wink:

1 Like

I just can tell you that i got it working on Android, with a HTML export and this “JavaScript” attached to it:

var URL_1 = 'music_Menu.mp3';
var URL_2 = 'music_inGame.mp3';
var buffer_1; // important
var buffer_2; // important
var audioCtx_1 = new (window.AudioContext || window.webkitAudioContext)(); // max. 6x are allowed
var xhr_1 = new XMLHttpRequest();
var xhr_2 = new XMLHttpRequest();
var startedAt; // timer save-slot
var pausedAt; // timer save-slot
var ALL_loaded = 0;  // in this case, there are 2x sound-files (ALL_loaded = 2;)
//-------------------------------------------------------------------------------------------------	
        xhr_1.open('GET',URL_1, true);
        xhr_1.responseType = 'arraybuffer';
        xhr_1.onload = function() { audioCtx_1.decodeAudioData(xhr_1.response, onBufferLoad_1);
        }; xhr_1.send();
		
function playBeginning_1() {
    source_1 = audioCtx_1.createBufferSource();
    source_1.connect(audioCtx_1.destination);
    source_1.buffer = buffer_1; // important
    source_1.loop = false; // true, false
    startedAt = Date.now(); // saves current time
    source_1.start(0);
};
function pauseResume_1() {
    source_1 = audioCtx_1.createBufferSource();
    source_1.connect(audioCtx_1.destination);
    source_1.buffer = buffer_1; // important
    source_1.loop = false; // true, false
    startedAt = Date.now() - pausedAt; // takes time from saved "pausedAt"
    source_1.start(0, pausedAt / 1000);
};

function onBufferLoad_1(b) {
    ALL_loaded = 1 + ALL_loaded ; // adds +1 to the "ALL_loaded" variable
    buffer_1 = b; // important
    playBeginning_1(); // plays imidiantly, after loaded
    source_1.stop(0); // prevent direct play & enabels direct trigger
};
//-------------------------------------------------------------------------------------------------	
        xhr_2.open('GET',URL_2, true);
        xhr_2.responseType = 'arraybuffer';
        xhr_2.onload = function() { audioCtx_1.decodeAudioData(xhr_2.response, onBufferLoad_2);
        }; xhr_2.send();
		
function playBeginning_2() {
    source_2 = audioCtx_1.createBufferSource();
    source_2.connect(audioCtx_1.destination);
    source_2.buffer = buffer_2; // important
    source_2.loop = false; // true, false
    startedAt = Date.now(); // saves current time
    source_2.start(0);
};
function pauseResume_2() {
    source_2 = audioCtx_1.createBufferSource();
    source_2.connect(audioCtx_1.destination);
    source_2.buffer = buffer_2; // important
    source_2.loop = false; // true, false
    startedAt = Date.now() - pausedAt; // takes time from saved "pausedAt"
    source_2.start(0, pausedAt / 1000);
};
function onBufferLoad_2(b) {
    ALL_loaded = 1 + ALL_loaded ; // adds +1 to the "ALL_loaded" variable
    buffer_2 = b; // important
    playBeginning_2(); // plays imidiantly, after loaded
    source_2.stop(0);// prevent direct play & enabels direct trigger
};
//-------------------------------------------------------------------------------------------------	

Frame_1D.onclick = function() {
playBeginning_1();
};

Frame_2D.onclick = function() {
source_1.stop(0);
pausedAt = Date.now() - startedAt; // save the current Playtime
};
Frame_3D.onclick = function() {
pauseResume_1();
};

});
audioCtx_1.resume(); // play again

Frame_4D.onclick = function() {
playBeginning_2();
};
Frame_5D.onclick = function() {
source_2.stop(0);
pausedAt = Date.now() - startedAt; // save the current Playtime
};
Frame_6D.onclick = function() {
pauseResume_2();
};

To start Playing, just type: playBeginning_1(); for example. And to stop : source_1.stop(0); . This works in the chrome browser. Even when you close the browser and even you press power and phone goes into stand-by. I hope it works, helps you …

1 Like

Thank you, very much @zl.kraljevic! :blush: I’ll check this out :smiley: