Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mp3 audio playback not working with Cordova 3.5 on iOS

Tags:

ios

cordova

audio

Recently I upgraded our iOS Project from Cordova 3.3 to 3.5.

mp3 files (that were previously downloaded into the iOS standard documents folder) will not play using the media API / plugin. This code has been working reliably on iOS for many versions up and including Cordova 3.3... The mp3 files have been downloaded into a subdirectory called 'Downloads' in the standard iOS App 'documents' folder..

In XCode console I get the following error:

Unknown resource 'file://localhost/Users/weeasle/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/{App-ID}/Documents/Downloads/testsound.mp3'

In my code (after a few blocks of code for the File Plugin API) I get the directory using: downloadDirFullPath = window.appRootDir.toURL();

To comply with the new changes in 3.5's file API, I recently changed this from downloadDirFullPath = window.appRootDir.fullPath;

Is the Media Plugin API for Cordova 3.5 broken for audio playback from the iOS App Documents directory???

Or am I just really tired and missing something obvious... Any suggestions or info greatly appreciated.

Chris aka weeasle

UPDATE ON 20/Jun/14: Fix Found. I can now get my downloaded .mp3's to play on iOS Cordova 3.5... Apparently with the new Cordova 3.5 filesystem the entry.fullPath method no longer works and is replaced by entry.toURL() as per https://github.com/apache/cordova-plugin-file/blob/master/doc/index.md

This works fine when calling images with Cordova, However there has been a quirk with the Media Plugin on iOS for quite some time: For sound playback, it does not accept URLs starting with file:/// it only takes absolute paths like /var/mobile/Applications/{GUID}/Documents/

The answer and fix is to use the new toInternalURL() method called within the entry returned from fileSystem.root.getDirectory. The exact call I use is downloadDirFullPath = window.appRootDir.toInternalURL(); (called from the entry passed by fileSystem.root.getDirectory).

This returns: cdvfile://localhost/persistent/Downloads/ which when pre-pended to .mp3 or other audio files plays successfully in iOS..

This also works for displaying images and media so it is a single reliable solution for Cordova iOS 3.5 and up..

This is by far the preferred solution since it is safer than using relative paths (as if in future Apple makes significant file structure changes with an iOS release, file access could break)...

One further note - in addition to the calls/methods above, downloadDirName = window.appRootDir.name; also sets cdvfile://localhost/persistent/ base path

Weeasle

like image 931
chris Avatar asked Jun 13 '14 12:06

chris


2 Answers

I haven't played around with media on iOS recently but if I look at the docs it states:

var myMedia = new Media("audio/beer.mp3")
myMedia.play()  
// first looks for file in www/audio/beer.mp3 then in 
<application>/documents/tmp/audio/beer.mp3

So, if I were you I'd try a relative path of:

"../Downloads/testsound.mp3"

which should change:

<application>/documents/tmp/../Downloads/testsound.mp3

into just:

<application>/documents/Downloads/testsound.mp3
like image 171
Simon MacDonald Avatar answered Oct 20 '22 16:10

Simon MacDonald


The only thing that worked for me with latest cordova 3.5 version is there: http://www.raymondcamden.com/2014/06/23/Cordova-Media-API-Example

Make sure to install this plugin: cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

like image 33
Christophe Avatar answered Oct 20 '22 15:10

Christophe