[SOLVED] Sound not working on iOS


#1

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?


#2

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);
    audiosource.play('mysound');
}

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

#3

Thanks! Works like a charm.


#4

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


#5

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:


#6

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?


#7

@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?


#8

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


#9

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.


#10

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;


#11

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