Cordova iOS App - Audio not playing and not full screen

Hello all,

I am searching the Cordova forums but figured i should ask here as well. I am porting one of my playcanvas games into an Apple App. To do this i am using Cordova as the wrapper. Mostly everything seems to be working but there are a few issues that hopefully someone encountered before and can give me some guidance. Again this is an apple app so these issues are on iOS

  • the app fills the screen i have on iphone 7 but on some new models (iphone 11) it has a boarder around it that looks like the web page background

  • for some reason i do not have any audio on the iOS app

  • if you hold down on one of the buttons iOS thinks you are trying to select something and the copy paste dialog comes up

any help would be greatly appreciated. Thank you

The default HTML and CSS currently doesn’t handle the notch.

The following is due to be added to the documentation and/or fixed as part of this fillmode API change.


To ensure you app runs in fullscreen on all iOS devices you’ll need to change a few things both in config.xml, the default Playcanvas css and index.html.

Add the following to your config.xml between the <platform name=iOS> tags to hide the status bar

<config-file parent="UIStatusBarHidden" target="*-Info.plist">
    <true/>
</config-file>
<config-file parent="UIViewControllerBasedStatusBarAppearance" target="*-Info.plist">
    <false />
</config-file>

This will be enough for most iOS devices, however some newer devices with notch will still show black bars at the top or bottom. Fixing this requires changes to index.hmtl and style.css. These changes are automatically done with the download script posted above, so you can skip the next changes if using.

Open up index.html and find the <meta name="viewport" tag. Add viewport-fit=cover so the the whole tag looks something like:

<meta name='viewport' content='width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, viewport-fit=cover' />

In style.css the html height is specified in %. Change this to vh:

html {
    height: 100vh;
    ...
}

Some users report that you also need to add launch storyboard images before you app will utilize the fullscreen on all devices. I didn’t find this to be neccesarry, but launch images should be added to avoid App Store rejection anyway. Info on how to add launch images can be found at: Splashscreen - Apache Cordova


IOS is very restrictive about HTML5 audio playback, so to ensure your audio works and plays reliably you can use a plugin to play audio natively. I’ll use the Media plugin from Cordova: Media - Apache Cordova

Install by running

corodva plugin add cordova-plugin-media

This adds a global Media object you can use to play sounds with a path to your sound. You can get the path to an asset with asset.getFileUrl(). The URL you get will contain a query string similar to: ?id=123&branchId=abc. Make sure you strip this off or the Media plugin will fail to locate the file. Playing a sound then looks like:

if(!window.cordova)
    return;

var path = asset.getFileUrl();
path = path.substring(0, path.indexOf("?"));

var media = new window.Media(path, null, (err) => { console.log("Error playing sound at", path, err}));
media.play();

Since Audio playback works on Android you can use the device plugin to only play audio natively if you’re on iOS. If you want to use the media plugin on Android you’ll to need prepend file:// to your asset path.

3 Likes

There’s a CSS fix for that: PlayCanvas 3D HTML5 Game Engine

The reason it’s not the default is because it affects apps that depend on HTML UI and DOMs

well 2 of the things were straight forward. I had to add

width: 100vh;

to the css as well.

my last thing is the audio. Is there no better way then having to refactoring the whole sound system to us cordova-plugin-media?

i asked in the cordova discord and got this response if this helps at all with a response:

This should be confirmed but i think the ios webview only allows audio playback from a user generated event, such as a trusted click event. Quickest way to test this is to add a regular dom node like a button or something and use .addEventListener('click', function() { myAudio.play(); }); and see if your audio works that way. If so, that may explain why your audio isn’t working. Events should have a trusted attribute which will be true if it came from an user (e.g wasn’t programmatically created).cordova-plugin-media would be a solution to that since it just uses the native APIs to play audio, thus doesn’t have such limitations.To your refactoring problem, I would probably suggest implementing your own interface that hopefully can conform to your existing API usage to minimize refactoring and your own audio implementation can just use the cordova apis behind the scenes. This also has an added benefit of decoupling a particular implementation to your app which makes it easy to reuse your code / change audio implementation easily.

1 Like

I am still not sure if i need to go down the route of cordova-plugin-media.
looking at the logs it looks like only the audio files are not found / loaded but all the other assets are? Seems wierd.

[Log] Powered by PlayCanvas 1.45.3 895add4 (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281348/1/AMBForst_Forest%20(ID%200100)_BSB.wav?t=76d2708cf6775841b0ce3cb39620abb0 - Error 0. Retrying in 200 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281349/1/AMBForst_Forest%20(ID%200100)_BSB.mp3?t=228386d66a859e40e3e75c9549c7a8d5 - Error 0. Retrying in 200 ms (playcanvas-stable.min.js, line 6)
[Warning] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.ogg (playcanvas-stable.min.js, line 6)
[Error] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.ogg
(anonymous function) (playcanvas-stable.min.js:6:643163)
(anonymous function) (playcanvas-stable.min.js:6:642393)
i (playcanvas-stable.min.js:6:599910)
[Log] GET: files/assets/52281348/1/AMBForst_Forest%20(ID%200100)_BSB.wav?t=76d2708cf6775841b0ce3cb39620abb0 - Error 0. Retrying in 400 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281349/1/AMBForst_Forest%20(ID%200100)_BSB.mp3?t=228386d66a859e40e3e75c9549c7a8d5 - Error 0. Retrying in 400 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281348/1/AMBForst_Forest%20(ID%200100)_BSB.wav?t=76d2708cf6775841b0ce3cb39620abb0 - Error 0. Retrying in 800 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281349/1/AMBForst_Forest%20(ID%200100)_BSB.mp3?t=228386d66a859e40e3e75c9549c7a8d5 - Error 0. Retrying in 800 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281348/1/AMBForst_Forest%20(ID%200100)_BSB.wav?t=76d2708cf6775841b0ce3cb39620abb0 - Error 0. Retrying in 1600 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281349/1/AMBForst_Forest%20(ID%200100)_BSB.mp3?t=228386d66a859e40e3e75c9549c7a8d5 - Error 0. Retrying in 1600 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281348/1/AMBForst_Forest%20(ID%200100)_BSB.wav?t=76d2708cf6775841b0ce3cb39620abb0 - Error 0. Retrying in 3200 ms (playcanvas-stable.min.js, line 6)
[Log] GET: files/assets/52281349/1/AMBForst_Forest%20(ID%200100)_BSB.mp3?t=228386d66a859e40e3e75c9549c7a8d5 - Error 0. Retrying in 3200 ms (playcanvas-stable.min.js, line 6)
[Warning] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.wav: Network error (playcanvas-stable.min.js, line 6)
[Error] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.wav: Network error
(anonymous function) (playcanvas-stable.min.js:6:643163)
(anonymous function) (playcanvas-stable.min.js:6:642393)
i (playcanvas-stable.min.js:6:599910)
(anonymous function) (playcanvas-stable.min.js:6:600447)
(anonymous function) (playcanvas-stable.min.js:6:28585)
(anonymous function) (playcanvas-stable.min.js:6:27619)
(anonymous function) (playcanvas-stable.min.js:6:26902)
(anonymous function)
[Warning] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.mp3: Network error (playcanvas-stable.min.js, line 6)
[Error] Error loading audio url: AMBForst_Forest (ID 0100)_BSB.mp3: Network error
(anonymous function) (playcanvas-stable.min.js:6:643163)
(anonymous function) (playcanvas-stable.min.js:6:642393)
i (playcanvas-stable.min.js:6:599910)
(anonymous function) (playcanvas-stable.min.js:6:600447)
(anonymous function) (playcanvas-stable.min.js:6:28585)
(anonymous function) (playcanvas-stable.min.js:6:27619)
(anonymous function) (playcanvas-stable.min.js:6:26902)
(anonymous function)

Seems some issue with audio not loading here too:

I don’t use Cordova but if I build a simple iOS app directly with xcode, with a web view with default settings and load a published playcanvas app hosted on playcanvas into the web view then audio files are loaded and play. This is with iOS 14.6 and PC 1.45.3.

In my iOS web view the sound played without any user interaction after loading the playcanvas app into the web view.

@kdarius I did a bit more investigation and I think it’s related to a permissions issue perhaps?

A workaround I’ve found is to encode the audio files into Base64 and written a script in the REST API tools as a branch: GitHub - playcanvas/playcanvas-rest-api-tools at cordova-publish-prep

I’ve tried this with Flappy Bird and it works a-ok :ok_hand:

Can you try this branch with your project and we can work from there?

Edit: I’ve tried this with Master Archer and that works as well.

1 Like

PR for updating the Cordova publishing steps using the REST API tools: Update cordova publishing steps to use the REST API tools by yaustar · Pull Request #288 · playcanvas/developer.playcanvas.com · GitHub

1 Like