p5.js-sound icon indicating copy to clipboard operation
p5.js-sound copied to clipboard

iOS 15, sound stop playing if Safari minimized or screen is locked.

Open estafaa opened this issue 2 years ago • 2 comments

On iOS 15 if you go for example to this example https://p5js.org/reference/#/p5.SoundLoop start playing audio and then minimize safari or lock the screen the audio "mutes" and continue laying one you unlock the screen or make safari active again. attaching a short video demo from the iOS 15 simulator, the similar thing on the actual phone.

html5 audio works with no problem, it keeps playing if I lock the screen.

https://user-images.githubusercontent.com/31907796/137606074-fd7902c3-3036-41a8-9a3d-4bddd0ef2ce3.mov

I submitted it to Apple feedback (bug report) https://feedbackassistant.apple.com/feedback/9709046 But I am not sure if that is a bug or some "new feature" of the apple. If you know some workaround, please share. Thank you!

estafaa avatar Oct 17 '21 01:10 estafaa

this seems to fix it:

//first make sure AudioContext is loaded, then set the state change
var checkAudContextInterval = setInterval(function(){
        if(typeof getAudioContext !== 'undefined'){
            getAudioContext().onstatechange = function() {
             // console.log(getAudioContext().state);
                 if (getAudioContext().state === 'suspended' || getAudioContext().state === 'interrupted') {
                  getAudioContext().resume();
                 }
              };
             clearInterval(checkAudContextInterval);
        }
    }, 1000);

estafaa avatar May 26 '22 19:05 estafaa

This might help

https://github.com/swevans/unmute

var setPhoneUnmute = false;
function touchStarted() {
  if (setPhoneUnmute == false && getAudioContext.state !== 'running') {
    setPhoneUnmute = true;
    unmute(getAudioContext());
  }
}

joepdooper avatar May 26 '22 21:05 joepdooper