Mobile Safari skips first seconds of HTML Audio on play()
Asked Answered
D

1

7

I have an React Audio Player that uses a useAudio hook to manage the HTML5 audio. It works fine everywhere except Mobile Safari, where the sound begins a few seconds after the player starts playing.

What's odd is that I'm checking canplaythrough on the element before calling HTMLAudioElement.play(). So Safari fires canplaythrough and starts "playing" the audio, except that the audio doesn't actually start until a second or so in.

Here's a complete example on CodeSandbox: https://codesandbox.io/s/useaudioplayer-jvftw?file=/src/useAudio.tsx

To replicate, open in Mobile Safari and play the Audio.

Decarbonize answered 29/1, 2021 at 10:8 Comment(4)
I've had the same problem and posted an almost identical question here: #66053332Tireless
(Try putting ‘JavaScript’ as a tag - might attract more attention).Tireless
@Tireless good ideaDecarbonize
Not having trouble in my version of Safari. When I click play, the "Bonjour" audio starts up almost immediately, including the techno-news beats beforehand.Acheron
F
4

In my case audio would play ok the 1st time.
And from the 2nd time and on, safari would skip some initial seconds.

Solved it by calling
myAudio.load();
just before
myAudio.play();

Frumpish answered 23/4, 2022 at 18:29 Comment(3)
Thank you - this finally solved my problem after hours of trial and error. It's not clear what the cause of this bug is, as my audio is triggered by a click event. Safari refusing to cache the audio?Bandog
You are a livesaver omgBoondocks
This did not solve my problem. I am experiencing the same bug (iOS Safari PWA) and when I set the audio.src to a subsequent file, it still clips even if I do load() before play(). The fix for me was here: #67630261Corsetti

© 2022 - 2024 — McMap. All rights reserved.