top | item 44063377

(no title)

matteason | 9 months ago

It's actually surprisingly hard to get iOS Safari to keep playing audio with the screen off.

When I made https://ambiph.one I ended up having to route everything through a MediaStreamAudioDestinationNode to trick Safari into thinking it's a livestream, which is apparently the only type of audio allowed to play in the background

Minimal demo here if it's helpful for anyone: https://codepen.io/matteason/pen/VYwdzVV

discuss

order

thorum|9 months ago

The solution I found after approximately two months of struggling with this problem: you have to generate an audio file that is a few seconds of silence, play it on a loop, and play it at the same time as the actual audio file you want to play (via separate audio elements, or an AudioContext). Specifically I believe you need to make sure the silence is “playing” at track boundaries for the real audio, so there is never a single moment where your webapp stops playing audio.

prmoustache|9 months ago

How do you even accept to use/and develop for a device that forces you to do that?

matteason|9 months ago

Ohhh that's interesting, so the root cause for my workaround working might be that the "live" audio node that I stream everything else to effectively never stops

busymom0|9 months ago

Does iOS let you play multiple audio at the same time?

egglemonsoup|9 months ago

Hey Matt! I've been a fan of Ambiphone for a while and I see your comments on HN surprisingly often. I've been trying to build a different web audio player with inspiration taken from yours. I haven't figured out the screen off audio thing, so thank you so much for sharing this demo!!!

matteason|9 months ago

Thanks so much, that's really cool to hear! Let me know if you ever hit any more problems, I've been meaning to blog about a bunch of problems I had to work round in various browsers but haven't got round to it yet, so happy to answer any questions