1
votes

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; 
};
1
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

1 Answers

0
votes

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');
}