Why can't JavaScript .play() audio files on iPhone safari?
JavascriptIosIphoneAudioSafariJavascript Problem Overview
I've got a JavaScript web app working that plays some audio periodically like this:
var SOUND_SUCCESS = new Audio('success.mp3');
SOUND_SUCCESS.play();
This works great on desktop browsers (tested in Edge and Chrome), but it doesn't play on Safari on iPhone.
I've looked around Stack Overflow, and I found some answers from a couple of years ago that it's not possible to play audio from Safari unless you're in that full screen player. Is this still the case?
Javascript Solutions
Solution 1 - Javascript
To add to xingliang cai's response, here's a code sample I got to work for me (edited below to work on iOS14, thanks @AndrewL!):
const soundEffect = new Audio();
soundEffect.autoplay = true;
// onClick of first interaction on page before I need the sounds
// (This is a tiny MP3 file that is silent and extremely short - retrieved from https://bigsoundbank.com and then modified)
soundEffect.src = "data:audio/mpeg;base64,SUQzBAAAAAABEVRYWFgAAAAtAAADY29tbWVudABCaWdTb3VuZEJhbmsuY29tIC8gTGFTb25vdGhlcXVlLm9yZwBURU5DAAAAHQAAA1N3aXRjaCBQbHVzIMKpIE5DSCBTb2Z0d2FyZQBUSVQyAAAABgAAAzIyMzUAVFNTRQAAAA8AAANMYXZmNTcuODMuMTAwAAAAAAAAAAAAAAD/80DEAAAAA0gAAAAATEFNRTMuMTAwVVVVVVVVVVVVVUxBTUUzLjEwMFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQsRbAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQMSkAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";
// later on when you actually want to play a sound at any point without user interaction
soundEffect.src = 'path/to/file.mp3';
Solution 2 - Javascript
iOS disables autoplay, instead requiring that play be initiated as part of a user interaction (e.g., you can start playback within a touchstart listener). There's a bit of documentation about this on Apple's developer documentation. There's also this article Overcoming iOS HTML5 audio limitations on IBM's developer site that has examples and more detail.
Solution 3 - Javascript
IOS on mobile disable automatic sound playing by default. So to get around this problem. You could put enable/disable switch button somewhere on the page and play some sound using an audio element("audioElement") if the user click the button switch.
After that, the same "audioElement" can be used to play future sounds by changing its "src" attribute and call its "play()" method, without any further user interaction.
Solution 4 - Javascript
To get @user2415116's solution to work in iOS 14, I did this:
const soundEffect = new Audio();
soundEffect.autoplay = true;
// onClick of first interaction on page before I need the sounds
// (This is a tiny MP3 file that is silent and extremely short - retrieved from https://bigsoundbank.com and then modified)
soundEffect.src = "data:audio/mpeg;base64,SUQzBAAAAAABEVRYWFgAAAAtAAADY29tbWVudABCaWdTb3VuZEJhbmsuY29tIC8gTGFTb25vdGhlcXVlLm9yZwBURU5DAAAAHQAAA1N3aXRjaCBQbHVzIMKpIE5DSCBTb2Z0d2FyZQBUSVQyAAAABgAAAzIyMzUAVFNTRQAAAA8AAANMYXZmNTcuODMuMTAwAAAAAAAAAAAAAAD/80DEAAAAA0gAAAAATEFNRTMuMTAwVVVVVVVVVVVVVUxBTUUzLjEwMFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQsRbAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQMSkAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV";
// later on when you actually want to play a sound at any point without user interaction
soundEffect.src = 'path/to/file.mp3';
Solution 5 - Javascript
I used this:
useEffect(() => {
window.addEventListener('touchstart', () => {
document.getElementById('audio').muted = false
document.getElementById('audio').play()
})
})
As soon as the user scrolls, the sound plays
Solution 6 - Javascript
Very old question but something that might help some people out, in my case (ofcourse this might not be viable in all cases), it helped to play all audio files for 1millisecond, then pause them. After that, if you resume playing the audio it works without issue.
playButton.addEventListener('click', function(){
introVid.play();
audioPlayers[0].play(); //every array element is constructed using new Audio("yourlink");
audioPlayers[1].play();
audioPlayers[2].play();
audioPlayers[3].play();
window.setTimeout(function(){
audioPlayers[0].pause();
audioPlayers[1].pause();
audioPlayers[2].pause();
audioPlayers[3].pause();
},1);
})
Solution 7 - Javascript
Safari gives priority to the soundtrack of videos over audio files! If a video has a soundtrack, even if it is silent and set to mute it will block the audio. The solution is to strip the soundtrack from the video, not record over it. I hope this helps some one! I lost days of work with this problem.
Solution 8 - Javascript
Haha, I outsmarted it like this.
Put in the page the audio tag with autoplay (true)
<audio id="beep" src={Assets.SOUND_BEEP} autoPlay />
It will play the sound once the element is mounted. (Even on safari iOS)
Then it seems you can play it whenever you want again by calling
document.getElementById('beep').play();
But now you may say, but I don't want to play the sound as autoplay.
Yes, I outsmarted it to put the "muted" property on that, and then set it to false when playing.
<audio id="beep" src={Assets.SOUND_BEEP} autoPlay muted />
And play:
document.getElementById('beep').muted = false;
document.getElementById('beep').play();