audio in android app fails to play - javascript

For some reason i do not know my audio files fails to play after compiling. I upload my files over to the build over some months ago and after the compilation, the audio was playing.
Just yesterday i upload the same files again with a little bit of changes but no changes made to the audio script and its failing to playing after the download.
I get this error code:1 message undefined
music.js
// JavaScript Document
`enter code here` // Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
//function onDeviceReady() {
// playAudio(cordova.file.applicationDirectory + "Playlist/music/Eminado.mp3");
// }
function play2face() {
playAudio('/android_asset/www/Playlist/music/African Queen.mp3');
}
// Audio player
var my_media = null;
var mediaTimer = null;
// Play audio
//
function playAudio(url) {
// Create Media object from url
stopAudio()
my_media = new Media(url, onSuccess, onError);
// Play audio
my_media.play();
}
// Pause audio
//
function pauseAudio() {
if (my_media) {
this.my_media.pause();
}
}
// Stop audio
//
function stopAudio() {
if (my_media) {
this.my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
HTML
Play Music | Stop Music
I've got cordova.js too in the head section of my index.html
also i have these plugins included in the config.xml
<plugin name="nl.nilscorver.cordova.media" spec="0.2.18" source="pgb" />
<plugin name="br.com.paveisitemas.splashscreen" spec="2.1.1" source="pgb" />
<plugin name="org.apache.cordova.inappbrowser" spec="0.6.0" source="pgb" />
<plugin name="com.surfernetwork.fileplugin" spec="1.0.2" source="pgb" />
any help on how to make it play?

I'm not a JS expert so I can't really help you there, but I will add that android has a built-in Music player class that you should use when playing audio. I'm sorry I can't help more :(

Related

Running a HTML5 Audio player in a Firefox Add-ons SDK page-worker

I am trying to repair a firefox addon that I created some time ago which has stopped functioning properly after a recent-ish Firefox update. The part which no longer functions was a mp3 player, which ran in the background.
The page-worker has a very small html page with an embedded media player and a script that controls the player. The script is passed in a filepath for an mp3 on my machine to use as the players src, and then told to start playing. This used to work, but now upon calling the embedded player's play function an error is thrown. The src is correctly set and the play event triggers, but the canplay event does not. The error code given is 4, which I believe is MEDIA_ERR_SRC_NOT_SUPPORTED.
If I open the HTML page and feed the player the filepath manually, it all works fine. What's going wrong here that wasn't before?
The HTML page is
<!DOCTYPE hmtl>
<html>
<head>
<title>Sound Stone: mp3 pageworker</title>
</head>
<body>
<video></video>
</body>
<html>
The content script is
var player = document.getElementsByTagName("video")[0];
var starttime;
var stoptime;
var checkTime;
self.port.on("start", function(url){
player.src=url;
player.pause();
stoptime = undefined;
self.port.emit("change", false);
});
self.port.on("stop", function(){
player.pause();
player.removeEventListener("canplay", goTo);
});
self.port.on("play", function(beg, end){
clearInterval(checkTime);
if(beg){
starttime = beg/1000;
}
if(end){
stoptime = end/1000;
}
player.play(); // this is where it goes wrong
if(beg){
player.addEventListener("canplay", goTo);
}
if(stoptime){
checkTime = setInterval(function (){
if(player.currentTime>=stoptime){
player.currentTime=99999;
clearInterval(checkTime);
}
}, 1000);
}
});
self.port.on("pause", function(){
player.pause();
});
player.onplay = function(){
self.port.emit("change", true);
}
player.onpause = function(){
self.port.emit("change", false);
}
player.onended = function(){
self.port.emit("done");
}
player.onerror = function(){
self.port.emit("out", player.error.code); // this outputs 4
self.port.emit("out", player.src); // this outputs as expected
}
function goTo(){
player.currentTime=starttime;
player.removeEventListener("canplay", goTo);
}
And a small excerpt of the main program is
var mp3 = worker.Page({
contentScriptFile: "./mp3.js",
contentURL: "./mp3.html",
});
mp3.port.on("out", function(msg){
console.log(msg);
});

Phonegap audio stops after playing 8 times

I have a simple mobile app designed using PhoneGap. I am some mp3 in it and I am using the Media plugin of PhoneGap. Everything is working fine, the audio is playing and so on.
The issue, I noticed after playing 8-12 times, the audio stops playing. I do not get sound. Here are my codes:
JavaScript
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
}
// Audio player
var media = null;
var audioP = false;
// Play audio
function playAudio(src) {
if (audioP === false) {
media = new Media(src, onSuccess, onError);
media.play();
audioP = true;
} else {
media.release();
}
}
// onSuccess Callback
function onSuccess() {
console.log("playAudio():Audio Success");
audioP = false;
}
// onError Callback
function onError(error) {
}
</script>
Help please!

PhoneGap media plugin stop the audio not working

I am workign with PhoneGap 3.0.0 and my confic.xml file does have the needed plugins. I can start saying that my sound do start when i run my code, and that everything else works. Just 1 thing dont want to work.
I am using Jquery Mobile.
I cant stop the audio.
I am calling a function called "stopAudio()" that read a global variable "my_audio" and fire of the "stop()" function.
Here is my code.
Index.html
Stop Audio
AudioHandler.js
document.addEventListener("deviceready", onDeviceReady, false);
var my_media = null;
function playAudio(url) {
try {
var my_media = new Media(url,
// success callback
function () {
my_media.release();
},
// error callback
function (err) {
my_media.release();
});
// Play audio
my_media.play();
} catch (e) {
alert(e.message);
}
}
function stopAudio() {
my_media.stop();
}
controller.js
$("#stopaudio").click(function() {
stopAudio();
});
Example usage:
playAudio("/android_assets/www/mysound.mp3");
So i have these 3 files. What i posted is a small amount of the content, but the other content in those files have nothing to do with the audio. I did also try and comment everything out and so forth.
So, basicly the "controller.js" is the file where the click are detected, and from there the function "stopAudio()" are called that comes from AudioHandler.js.
My audio do start on startup of the phone.
I cant stop the audio.
Im running out of ideas, any help would be welcome :-)
Thanks :)
You've declared my_media as a local variable here:
try {
var my_media = new Media(url,
You want it to be global, so remove the var:
function playAudio(url) {
try {
my_media = new Media(url,
// success callback
function () {
my_media.release();
},
// error callback
function (err) {
my_media.release();
});
// Play audio
my_media.play();
} catch (e) {
alert(e.message);
}
}

resume audio phonegap

hey guys i am using the media phonegap api to play,pause and stop the audio. so far i managed to play,stop and pause, but i do not know how to resume please help me
below is the code i have written till now
var my_media = null;
var mediaTimer = null;
var pausePos = 0;
var counter=0;
var playing=false;
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// Play audio
my_media.play();
// get audio duration
var duration = my_media.getDuration();
// set slider data
if( duration > 0 ){
$('#slider').attr( 'max', Math.round(duration) );
$('#slider').slider('refresh');
}
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition(position);
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
/* pause audio */
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
function resumeAudio()
{
}
/* stop audio */
function stopAudio() {
if (my_media) {
my_media.stop();
playing = false;
my_media.release();
}
clearInterval(mediaTimer);
mediaTimer = null;
pausePos = 0;
}
// onSuccess Callback
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
function setAudioPosition(position) {
pausePos = position;
position = Math.round(position);
$('#slider').val(position);
$('#slider').slider('refresh');
}
function player(id)
{
//alert(id);
playAudio("/sdcard/MuseumCache/"+id+"/"+id+".mp3");
//alert("end");
}
Please help me to write the resume function. they said the media.play() plays and resumes playing audio file
ok I fond the solution: Phonegap's Docs are terrible...badly explained
so their example is sloppy
the point is: you must not reinitialize the media everytime you play otherwise it starts form beginning
var my_media == null;
function playAudio(url) {
if (my_media === null){
// so only is your media handler is empty you initialiaze it otherwise you just play
my_media = new Media(url, function() {
console.log("Audio Success");
}, function(err) {
console.log("Audio Error");
});
}
my_media.play();
}
As per the documentation of PhoneGap media.play starts or resume the audio file.
media.play: Start or resume playing audio file.
you can find the link here
Francesco your solution work only if you have one sound to play, if you have multi sound on the same page each player will get the property of the first media...

audio not working with phoneGap

Hi i try to add sound to my game i used aphoneGap code to do it :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Media Example</title>
<script type="text/javascript" charset="utf-8" src="js/phonegap-1.4.1.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
playAudio("http://audio.ibeat.org/content/p1rj1s/p1rj1s_-_rockGuitar.mp3");
}
// 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();
// Update my_media position every second
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// get my_media position
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
}
},
// error callback
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
// Pause audio
//
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
// Stop audio
//
function stopAudio() {
if (my_media) {
my_media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
// onSuccess Callback
//
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
//
function setAudioPosition(position) {
document.getElementById('audio_position').innerHTML = position;
}
</script>
</head>
<body>
<a href="#" class="btn large"
onclick="playAudio('http://audio.ibeat.org/content/p1rj1s/p1rj1s_-
_rockGuitar.mp3');">Play Audio</a>
Pause Playing Audio
Stop Playing Audio
<p id="audio_position"></p>
</body>
</html>
it works fine , but when i change the source of sound it didn't work here's the code :
playAudio("file:///android_asset/www/sounds/introDino.mp3");
Any idea please,
Thank you in advance
Change your path to "/android_asset/www/sounds/introDino.mp3" and it will be fine. Read my mini tutorial for Android:
http://simonmacdonald.blogspot.com/2011/05/using-media-class-in-phonegap.html
This function will play sound using the HTML5 Audio API if available, with fallback to the PhoneGap Media API if not:
function playAudio(src) {
// HTML5 Audio
if (typeof Audio != "undefined") {
new Audio(src).play() ;
// Phonegap media
} else if (typeof device != "undefined") {
// Android needs the search path explicitly specified
if (device.platform == 'Android') {
src = '/android_asset/www/' + src;
}
var mediaRes = new Media(src,
function onSuccess() {
// release the media resource once finished playing
mediaRes.release();
},
function onError(e){
console.log("error playing sound: " + JSON.stringify(e));
});
mediaRes.play();
} else {
console.log("no sound API to play: " + src);
}
}
Due fact that Audio object on new Android versions (may be on other platforms) available and not 'undefined' both previous answers will not work. So I upgraded code to :
function playSound(src){
var devicePlatform = (typeof device !== 'undefined' && device.platform)? device.platform: null;
if (typeof Audio !== "undefined" && devicePlatform === null) { // HTML5 Audio
new Audio(src).play() ;
} else if ( devicePlatform ) { // Phonegap media
if (devicePlatform === 'Android') { // Android needs the search path explicitly specified
src = '/android_asset/www/' + src;
}
var media = new Media(src,
function onSuccess() {
media.release(); // Release the media resource once finished playing
},
function onError(e){
console.log("error playing sound: " + JSON.stringify(e));
}
);
media.play({numberOfLoops: 1});
} else {
console.log("no sound API to play: " + src);
}
}
Also must say that before all you must add to cordova build 'device' and 'media' plugins otherwise nothing will work. Plugins available here

Categories