Play Sound on Phonegap App for Android

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 Build App - Play Audio

Try to install the cordova Media plugin org.apache.cordova.media (http://docs.phonegap.com/en/edge/cordova_media_media.md.html)

And then, try this :

//src is your path to your file
var my_media = new Media(src, function(){
alert("Success");
}, function(){
alert("Fail");
});

my_media.play();

Try it with all the paths yuo already tried, but this plugin works for me with online files.

EDIT: You may need to add '/android_asset/www/' in front of your src

Sound plays in PhoneGap when testing with phonegap app, but not when built with PhoneGap Build

The changes I made were instead of new audio(); I did new Media(); and after reading Playing Local Sound In Phonegap I found out it may have had something to do with the path needing to be an absolute local path, it works in the real packaged app now. I also discovered you need too use media.release(); because of the finite number of times you can play a sound.

$('.sound1').on('touchstart', function () {
var path = window.location.pathname;
path = path.substr( path, path.length - 10 );
var finalpath = 'file://' + path;
var key = $(this).attr('key');
var thissound = new Media( finalpath + key, function onSuccess() {
// release the media resource once finished playing
thissound.release();
} );
thissound.play();
});

Audio will not play on Android using phonegap, but works fine on iOS

I finally managed to get it working!!

I first initialising the audio file by finding the full Android path using @Sarim Sidd help above and then preloading the audio file:

//INITIALISE SOUND
if(device.platform === "Android") //DIFFERENT PATH FOR ANDROID
{
//navigator.notification.beep(1);
var path = window.location.pathname;
path = path.substr( path, path.length - 10 ); //strip off index.html
url = 'file://'+path+"sound.ogg";
}else{
url = "sound.mp3";
}
//sound = new Media(url);
sound = new Media(url);
sound.play();
sound.pause();

Then when to play the audio I forced the volume to full (1.0) before calling the play method. For some reason I noticed the audio kept reseting to mute each time I started the app:

//PLAY BELL SOUND THROUGH APP
sound.setVolume(1.0);
sound.play();

playing audio sounds in Cordova application

To answer your question, you can find the working sample of cordova app using Media Plugin in the following github page.

As mentioned in the sample project's README, you gotta install cordova device plugin as well to check the device platform.

Also to clarify the doubt you mentioned in the comment, android_asset refers to the project's root folder.



Related Topics



Leave a reply



Submit