Cordova Media API Example

A few months ago I launched a new GitHub repo ( as a way to try to collect my various Cordova examples together under one roof. I had planned to add to it regularly but – life – as you know – gets in the way. I’ve finally gotten around to adding another example, this one for the Media API.

Specifically, my example looks at how to play a MP3 file that is shipped with your application. You would think this would be trivial. Assuming I’ve got a folder under www called sounds and a MP3 file called something.mp3, you would think this would do it:

var media = new Media("sounds/something.mp3", null, mediaError);;

Unfortunately, this breaks in Android. Why? Because Android. (No, no, I kid, sorry.) Specifically Android requires slightly different “pathing” to get to the same source. Specifically it requires this in front of the path: /android_asset/www/

So what I did was write a simple utility function that makes use of the Device API. Consider:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {
	document.querySelector("#playMp3").addEventListener("touchend", playMP3, false);

function playMP3() {
    var mp3URL = getMediaURL("sounds/button-1.mp3");
    var media = new Media(mp3URL, null, mediaError);;

function getMediaURL(s) {
    if(device.platform.toLowerCase() === "android") return "/android_asset/www/" + s;
    return s;

function mediaError(e) {
    alert('Media Error');

All I’ve done is sniff for the Android platform and modify the passed-in path value to add the Android prefix. I did not test this outside of iOS and Android so I can’t say if it will work in Blackberry or Firefox, but, it gives you a basic idea of how to use a MP3 in your application.

You can find the source for this particular project here: As a reminder, you can clone the entire repository, and when you make a new Cordova project, simply use the –copy-from attribute to copy in my source code.

  • Steven Benjamin

    Call the release() in the media success callback.

  • Steven Benjamin

    Ray, I think if you use the file API this will manage the file locations for you.

    for example: fileSystem.root.getFile(“recording.mp3”, { create: false, exclusive: false }, _gotFileSuccess, _gotFileError);

    • http://www.raymondcamdencom/ Raymond Camden

      Hmm, good point. I’d probably still go with my workaround as it requires one less callback and one less plugin. Although what you have may be more future proof too. :)

  • David villota

    you are the best with your code i can use this service on my aplication !! thanks a lot for your post you help a lot dont you even imagine how much!! thanks!

  • J Lee

    It might be worth pointing out, Cordova’s media plugin does not work with .wav files but .mp3 files.

  • PLa5teRz

    Hi, I just try your code, but can you put some sample with pause and stop button. Thank you

    • http://www.raymondcamdencom/ Raymond Camden

      For the most part, they just work as is. Are you having trouble with them?

      • Colin Hamilton

        Is there a solution to play local mp4? Nexus 7 seems to work for etc but not for Galaxy tab. this media API only works for audio? why not video ?

        • http://www.raymondcamdencom/ Raymond Camden

          I’m confused – are you using the API or video tags?

  • Luis Troya

    Do you have an example where you record audio using this plugin?

  • Taehwa

    You saved my life

    • http://www.raymondcamdencom/ Raymond Camden

      Happy to help. :)

  • Android Example

    hi dear friends you learn the android example of Play Video File – Android Example