Relative media source in PhoneGap

audio, cordova, cross-platform

Solution

The Media API is not properly normalized across all OSes that PhoneGap runs on. If you want to play something from the assets folder on Android, you will need to specify the full path. You may be better off using the device.name property to detect whether you are on Android and prepending the /android_asset/www/ path.

Problem

I want to develop a kind of media player with PhoneGap (Cordova 2.2.0), and use Adobe's Phonegap Build for other platforms. This means almost everything has to be relative ;) The development of my app is on Android 2.3 (the minimal target). Here is my problem: The path to file works well when it's from the net (`http://...`), or has been given the definite path as shown below (`/android_asset/www/media/song.mp3`). However, when trying to do it relatively (`media/song.mp3`) the song won't start playing, giving me an error code: MediaPlayer: error (1, -2147483648) If I press play again I get a few more errors: MediaPlayer: start called in state 0 //meaning there is no file MediaPlayer: error (-38, 0) MediaPlayer: Error (-38,0) Is there a way to make it work relatively, without the need for: ``` if(Android){ src=... } if(iOS){ src=...; }... ``` The above option is time consuming, and will become error prone if the paths change for one of the platforms. Here's the minified Javascript Code: ``` var song; var app = { initialize : function() { this.bindEvents(); }, bindEvents : function() { document.addEventListener('deviceready', this.onDeviceReady, false); }, onDeviceReady : function() { app.receivedEvent('deviceready'); song = new Media("/android_asset/www/media/song.mp3"); console.log("The song is: " + (song.mediaStatus!=0?"okay":"none")); $('#play').click(function() { song.play(); console.log("Song started"); }); $('#pause').click(function() { song.pause(); console.log("Song paused"); }); $('#stop').click(function() { song.stop(); console.log("Song stopped"); }); }, receivedEvent : function(id) { console.log('Received Event: ' + id); } }; ``` If anything more is needed to say, please tell me.

Original source