Sound not playing on html5 target (ios)

In android and windows browsers, the sound plays fine. But when the webpage is opened in an iPhone, there is no sound.

Found some articles saying that the audio context has to be unmuted by a touch event in ios:

but the problem is how to get the audio context used by OpenFL/Lime?

I had the same problem. You must play any sound on any mouse down event first (click might work too), then all other sounds should be unmuted.

Great, hopefully we can think of a generic way to run this automatically from OpenFL, in order to make iOS give us sound :wink:

This looks like a good article for a lightweight way to enable it, we just need to think of how we would use it

Can you share some actual code?

I tried the following:

var snd:Sound = null;
stage.addEventListener(MouseEvent.MOUSE_DOWN, function(_)
	if (snd != null);
		Assets.loadSound("someSound.ogg", function(s) snd = s;);

So at the first click/touch, the sound is loaded. And at the second (and onwards) click/touch, the sound is played. This worked on android but still not giving me sound on iOS.

Can you check if sound works for you in my game?

startButton.addEventListener(MouseEvent.MOUSE_DOWN, startGame); private function startGame(e:MouseEvent):Void { var click:Sound = Assets.getSound('assets/sound/click.mp3'); }

That’s it. After playing single sound on mouse_down function all other sounds work fine…at least for me. I’m on OpenFL 2.0, I’m not sure if it makes any difference.

If it doesn’t work, you can try this:

untyped window.addEventListener('touchstart', touchstart);
private function touchstart():Void
     untyped window.removeEventListener('touchstart', touchstart);
     var sound:Sound = Assets.getSound(...);;

Well, I tried your game and it worked on my iPhone.
What asset you are using? ogg or mp3?

I have both mp3s and oggs, two files for each sound. Unfortunately you need both formats if you want to target most of the browsers.

finally I made it working by using mp3 files (I was using ogg and it did not work).

Btw, which browsers support mp3 and which support ogg and how do you decide which format to load/play?

You don’t have to decide manually which format to play. All you have to do is keep both formats in the same folder with the same filename. I’m always using Assets.getSound(‘sound.mp3’) and never had any problems.
As far as I know some versions of Firefox and Opera don’t support mp3, while IE, Chrome and Safari all do but I could be wrong.

Ok I kept both format in the same folder and everything is working like a charm now.

btw, I think Chrome on Windows is using ogg while Safari on ios is using mp3.

You do need to have a touch event (as mentioned above by hcwdikk) that triggers the code, usually a start function.

iPhone/Safari is not capable of playing OGG files, so you will need to use either MP3 or M4A (better format). Both formats will work in any web browser. OGG is the better choice overall, but is currently only supported in Chrome and Firefox. M4A is the next best choice.

1 Like