Yerram Naveen
Yerram Naveen

Reputation: 286

How to play a local audio file using phonegap

I am new to phonegap and am trying to play a local audio file when user clicks on play button and pause the audio when user clicks on pause button...

and am adding the path 
1) playAudio("/android_asset/fire.mp3");

But it is showing an error in logcat 

 04-17 07:17:20.221: E/Web Console(4558): ReferenceError: Can't find variable: Media at      file:///android_asset/www/index.html:29

i placed my audio file in asset folder

and I am checking on real device HTC android

Upvotes: 1

Views: 3381

Answers (3)

mshahidjanjua
mshahidjanjua

Reputation: 1

In case if you have a folder named audio in www folder. You may access the local file test.mp3 in it using the code.

media = new Media('audio/test.mp3');

Upvotes: 0

Yerram Naveen
Yerram Naveen

Reputation: 286

Give src = /android_asset/www/mp3filename.mp3 check it once you may be given

/android_assets/www/mp3filename.mp3

Upvotes: 0

Devgeeks
Devgeeks

Reputation: 5647

You might be trying to call Media before the deviceready event fires.

Something like the below might help:

<script type="text/javascript">

var myMedia = null;
document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady () {
    myMedia = new Media("/android_asset/www/fire.mp3", 
        function(){
            if (myMedia) {
                myMedia.stop();
                myMedia.release();
            }
        }, 
        function(error){
            console.log(error.message);
        }
    );
}

</script>

Upvotes: 3

Related Questions