Playing Local Sound in Phonegap

Playing local sound in phonegap

Try giving an absolute local path. E.g.

new Media("/android_asset/www/test.wav");

That should work on Android. Hopefully they'll fix this in PhoneGap, as it's something of a bug in the cross-device support.

Play sound on Phonegap app for Android

The play method which the HTML5 API provides is useful for playing media files available on the web. Phonegap provides the media plugin to play local media files. I checked out your code and played the sound successfully by making the following changes. Please check at your end.

1.Add the following line to config.xml

<gap:plugin name="org.apache.cordova.media" />

2.Replace the lines in index.html

<button onclick="document.getElementById('successSound').play()">Play successSound local</button>
<button onclick="document.getElementById('errorSound').play()">Play errorSound local</button>

with these lines

<button onclick="playAudio('successSound')">Play successSound local</button>
<button onclick="playAudio('errorSound')">Play errorSoundlocal</button>

3.Add the following function to js/index.js

function playAudio(id) {
var audioElement = document.getElementById(id);
var url = audioElement.getAttribute('src');
var my_media = new Media(url,
// success callback
function () { console.log("playAudio():Audio Success"); },
// error callback
function (err) { console.log("playAudio():Audio Error: " + err); }
);
// Play audio
my_media.play();
}

Phonegap - unable to play local sound file

getDuration returns -1 when you call it synchronously. You need to call it in a setInterval loop like shown here http://docs.phonegap.com/en/1.9.0/cordova_media_media.md.html#media.getDuration to get the correct duration.

I don't think you can play files from assets/www, but files on the sdcard work perfectly fine. You need to make sure that you don't pass the full path from the FileEntry object. You need to trim off the starting path (file:///mnt/sdcard/) since the media player plugin adds that by itself. I found that out after an hour or so of head scratching!

How to play song in Android - phonegap

PhoneGap for Android lets you store local audio files in two places:
android_asset: file name must start with /android_asset/sound.mp3
sdcard: file name is just sound.mp3

If you place your DANCE.mp3 file in your project's assets directory, then you can play it using the path:

mp3file = new Media("/android_asset/DANCE.mp3",
function() {
alert("playAudio():Audio Success");
},
function(err) {
alert(err);
}
);
mp3file.play();

(You can read the source for AudioPlayer.java in the Android PhoneGap sources to see how this works.)



Related Topics



Leave a reply



Submit