Audio.play not working with iOS

Hey all,

I’m working on a website for a classmate that has an A tag that on click, I have a jqeury event listener that does a .play() function in the background. It works on PC, but doesn’t play when I view the site on my phone. Thoughts?

I think this kind of issue with audio on apple devices is somewhat common. I know that apple will not allow audio that plays automatically when the page loads (I can somewhat understand why), but it should work if the audio is being triggered by a user action like a click…

Are you putting an <audio> element in the DOM or are you doing everything with javascript?

@RadDog25 everything in JavaScript. Is there a solution to this?

Maybe… It’s worth a try to use audio elements instead of just javascript…

<audio id="my-audio" >
          <source src='/your-audio-files/something.mp3' type="audio/mp3" />
</audio>

then in javascript

var myAudio = document.getElementById("my-audio");
myAudio.play();

Again, this will probably only work with iOS if triggered by user actions

@RadDog25 That doesn’t seem to work. I have what you’ve wrote but the audio file comes up as null

@SkyC

Well the way I used before was I imported the MP3 right into the javascript and didn’t mention it in the html, and the second time around I tried @RadDog25 's way and it didn’t even work on desktop, but haven’t tried on mobile