I have a problem with playing audio on IOS devices ... the code I use is this to activate/deactivate the sound:
<audio id="audio">
<source src="sounds/fart.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>
<button id="audio-btn" class="audio-btn" onclick="toggleAudio()">
<img id="audio-icon" src="img/sound-off.webp" alt="audio_play/pause" />
</button>
let audioIconPlay = false;
function toggleAudio() {
const audioIcon = document.getElementById('audio-icon');
audio.autoplay = true;
audioIconPlay = !audioIconPlay;
if (audioIconPlay) {
audioIcon.src = 'img/sound-on.webp';
} else {
audioIcon.src = 'img/sound-off.webp';
}
}
And this when I click the button to play the audio:
<button
onmousedown="playMyAudio()"
onmouseup="playMyAudio()"
>
Fart
</button>
function playMyAudio() {
if (audioIconPlay && audio.paused) {
audio.play();
} else {
audio.pause();
audio.currentTime = 0;
}
}
Ps .: I did it all on onTouch as well
Now, on android devices it works, every click plays the audio, while on iOS it works only on the first click! There is a solution? I tried this: Allow audio.play() on safari for chat app but it does not work.