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.
Complete HTML/CSS Course 2022 To play sound file in the background on a web page, use the <embed>… </embed> element. Also, use the autoplay attribute. This will run music in the background whenever the page loads.
The HTML <audio> element is used to play an audio file on a web page.
Audio pause() Method The pause() method halts (pauses) the currently playing audio. Tip: This method is often used together with the play() method. Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio).
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 callingmyAudio.load();
just beforemyAudio.play();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With