1

I've searched the internet high and low, have tried every recommendation and still no luck. I'm very resourceful and this has completely stumped me. I'm using Phonegap build, then I download to my Android Galaxy S4. I have had it work in Ripple, but even then no luck on the phone. My audio files are located in asset/www/audio/

The consistent error I receive is: Uncaught ReferenceError: Media is not defined I have tried every file structure and still no dice. Including what most answer call for

file:///android_asset/www/audio/horn.mp3

/android_asset/www/audio/horn.mp3

Included in config.xml(in regards to this issue):

<preference name="phonegap-version" value="3.5.0" />
<gap:plugin name="org.apache.cordova.media" version="0.2.13" />

First the call (have multiple places to call different audio files)

<a href="#" data-transition="flip" data-role="button" onClick="audioPathOne();">Start</a>
<a href="#" data-transition="flip" data-role="button" onClick="audioPathTwo();">Start</a>

In my js file...

function audioPathOne () {
        playAudio(getPhoneGapPath () + 'audio/beep.mp3');
}

function audioPathTwo() {
        playAudio(getPhoneGapPath () + 'audio/horn.mp3');
}

function playAudio() {
        my_media = new Media();
        my_media.play();

function pauseAudio() {
        if (my_media) {
            my_media.pause();
        }

function stopAudio() {
        if (my_media) {
            my_media.stop();
        }
        clearInterval(mediaTimer);
        mediaTimer = null;
    }

function onSuccess() {
        console.log("playAudio():Audio Success");
    }

function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
}

function getPhoneGapPath() {
var path = window.location.pathname;
path = path.substr( path, path.length - 10 );
return 'file://' + path; 
};
Nic Clark
  • 33
  • 3
  • You should read more about javascript. Uncaught ReferenceError: Media is not defined is a error. Media object not available. It don't rely to path. Please find and fix it, make sure Media object available then it will wroking – Hanh Le Oct 08 '14 at 01:54

1 Answers1

0

The correct path for media in Android is

/android_asset/www/

Its not your existing file structure, than can work in Ripple. Its the android path.

In the html:

<a href="#" data-transition="flip" data-role="button" onClick="playAudio('http://www.noiseaddicts.com/samples/4928.mp3');">Online</a>
<a href="#" data-transition="flip" data-role="button" onClick="playAudio('/android_asset/www/audio/horn.mp3');">Local</a>

The js file:

// Audio player
//
var my_media = null;
var mediaTimer = null;


// Play audio
//
function playAudio(src) {
    // Create Media object from src
    my_media = new Media(src, onSuccess, onError);

    // Play audio
    my_media.play();

}

// Pause audio
//
function pauseAudio() {
    if (my_media) {
        my_media.pause();
    }
}

// Stop audio
//
function stopAudio() {
    if (my_media) {
        my_media.stop();
    }
}

// onSuccess Callback
//
function onSuccess() {
    console.log("playAudio():Audio Success");
}

// onError Callback
//
function onError(error) {
    alert('code: '    + error.code    + '\n' +
          'message: ' + error.message + '\n');
}
xumet
  • 216
  • 1
  • 9
  • After phonegap build the "asset" folder is named asset. Should it be changed to android_asset? android_asset shouldn't have to be entered anyways due to using window.location.pathname correct? – Nic Clark Oct 07 '14 at 14:59
  • Thanks for you input xumet. Still the same issue. – Nic Clark Oct 07 '14 at 16:06
  • First: Is the Media Object working? Try an online file, for instance http://www.noiseaddicts.com/samples/3724.mp3 Second: Are you calling the function getPhoneGapPath or my function directly? – xumet Oct 07 '14 at 16:11
  • And don't forget the first **slash** in '/android_asset/www/' – xumet Oct 07 '14 at 16:19
  • All edit the answer with ALL the Javascript you need. Its a copy paste from a working project. You can test Online and Local with direct access from the HTML. – xumet Oct 07 '14 at 16:34
  • thank you again for you quick responses. I'm going to mess around with it and see if I can get it working. – Nic Clark Oct 07 '14 at 19:22
  • I had/have the same problem. My code was slightly different and playing online but not locally. I made my code match xumet's exactly and then nothing worked, but **I yanked that code out of onDeviceReady and made the function definitions and the two variables global, and now I have everything working for online files**, including my own file which I renamed to horn.mp3 to match the code, just to minimize the differences. But when I try to play `/android_asset/www/audio/horn.mp3`, it runs through the code but comes back with code 1 message: undefined in the alert box. – Andrew Stollak Oct 07 '14 at 19:35
  • When researching this, I found [this StackOverflow question](http://stackoverflow.com/questions/6186866/java-io-filenotfoundexception-this-file-can-not-be-opened-as-a-file-descriptor) about file compression with Java. I'm not sure if that matters. – Andrew Stollak Oct 07 '14 at 19:36
  • Ok, this worked once I installed the app from build.phonegap.com, but not when I used the PhoneGap phone app, and don't get me started on Ripple.... – Andrew Stollak Oct 07 '14 at 20:07
  • @nuke-stollak as you can read in the answer of the post you referral the mp3 is already compressed, It should not matter. You are right about the scope and the onDeviceReady: I edit the answer in order to clarify it. And remember: It NOT work in Ripple. – xumet Oct 08 '14 at 08:31