[SOLVED] Sound not working on iOS

I have tried to put music on my project and works perfectly on desktop and Android but not on iOS (tried on iPad and iPhone 5 with iOS 9.2). Despite that is very simple to play a sound file on an scene, I looked for a tutorial that helps me to know if I were doing something wrong. I’ve tried with the Basic Audio tutorial (http://developer.playcanvas.com/en/tutorials/beginner/basic-audio/) with same results, thats means, work ok on desktop and Android but not on iOS.

Is this a known issue?

iOS does not allow sounds to be played unless you start playing them as a result of user interaction. So you could do something like:

var audiosource = this.entity.audiosource;
var startSound = function () {
    document.removeEventListener('click', startSound);

var isIOS = /iPad|iPhone|iPod/.test(navigator.platform);
if (isIOS)
    document.addEventListener('click', startSound);
1 Like

Thanks! Works like a charm.

1 Like

Hi guys, I do have the same issue. I don’t know if something has changed in this 3 years but I copied and pasted the code with no result. Could anybody help me out? Is there a particular spot where to put the code?

Thank you

The engine should handle this for you now:

@vaios actually fixed this just after he made the suggestion above so you don’t need to add any code:

I was happy after your comment but I tried even this tutorial scene https://playcanvas.com/editor/scene/440346 and nothing worked but only on the Ipad pro. Good news is working on the ipad air 2 mini after touching the screen. Is there something in the security settings that doesn’t allow play the sound?

@petru23 I have just tested that scene on the iPhone X and the iPhone XS and both function as expected. Maybe there’s some bug in Safari on iPad Pro? Can you get any non-PlayCanvas WebGL apps with audio working on your iPad Pro?

Also, what version of iOS is your iPad Pro running? Latest?

None of what I tried is working, I tried Unity3D but Unity WebGL is not currently supported on mobiles.
I got IOS 12.3.1 which is the latest.

I found the solution, there is a way to override the autoplay ban but you need to adjust your Xcode file in order to do that.

You just need to add the line in your view controller.m file

self.Webview.mediaPlaybackRequiresUserAction = false;

1 Like

Oh! Sorry, I didn’t realize you were running in a WebView rather than mobile Safari! Well done for tracking down the problem. :smile:

1 Like