goldfire / howler.js

Javascript audio library for the modern web.
https://howlerjs.com
MIT License
24.04k stars 2.24k forks source link

Firing onpause event from iOS Control Center #984

Closed aareeph closed 6 years ago

aareeph commented 6 years ago

I am trying to listen to onpause event when user presses the Pause button on iOS Control Center but it doesn't fire the onpause event. I am trying it on iOS Safari.

Is there a way to listen to the even from iOS Control Center?

Thanks!

themoonrat commented 6 years ago

The onpause event only fires if you specifically say 'pause' on a sound. It doesn't happen automatically when you press the home screen. Take a look at the Page Visibility API - you can detect when the page is not longer visible and then pause / mute any sounds yourself

aareeph commented 6 years ago

Thanks for your response @themoonrat.

I am sorry I wasn't clear about my question. Please find the attached image of iOS Control Center. My audio is Play/Resumed when I press the Play/Pause button in Control Center but the onpause event doesn't fire up in the code. img_4056

I need to let the audio play in background and therefore I think Page Visibility API wouldn't me of much use in my scenario.

Thanks!

themoonrat commented 6 years ago

ahhh, sorry, misunderstood.

I believe this may be an issue with what iOS is doing. When you are pausing / resuming via the control centre, you are not going via the Howler API, you are going via the os. Therefore Howler events won't be emitted

I think this is the case :) Try using html5 setting enabled?

aareeph commented 6 years ago

Yes I've Html5 enabled, but it's still not working as expected.

I noticed in SoundCloud website that the song Play/Pause from the Control Center and it's state is also reflected in the web page immediately. So I think there's some way to fire events to Safari. Right now I am not sure which audio library they are using but I think they have handled this scenario well.

Thanks! :)

aareeph commented 6 years ago

For me window.requestAnimationFrame() and Howler's seek() method did the trick. My code keeps watching audio position and as soon as I start receiving same seek() position for few seconds it means that the audio is paused from iOS Control Center.