phonegap media sound stop playing after 5 times - android

I need to play the "correct" sound if user answer the question correctly and "wrong" if incorrect. The sound is playing at first but after at the fifth time I answer correctly which output the fourth time "correct" sound already the fifth time, no sound can be heard. It seems the music can only played at most 4th times.
What is the possible reason that cause this?
updated*
I added media.release in my stopAudio() but so far the sound can be heard for 6th (improved) but it is still not suits my case. (Many questions that need the same sound effect)
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, null, soundCB);
my_media.play();
}
function soundCB(err){
console.log("playAudio():Audio Error: "+err);
}
// Stop audio
//
function stopAudio() {
if (my_media) {
alert("AQA");
my_media.stop();
my_media.release();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
/////question part//////
$("#answer").live('tap', function(event){
if(buttonAns==true) {
$this= $(this);
buttonAns = false;
var choice = $this.attr("class");
if((correctAnsNo == 0 && choice =="answer0")||(correctAnsNo == 1 && choice =="answer1")||(correctAnsNo == 2 && choice =="answer2")){
playAudio('/android_asset/www/audio/fall.mp3'); //falling sound
correct = parseInt(correct)+2;
playAudio('/android_asset/www/audio/correct.mp3'); //dingdong
}else{
playAudio('/android_asset/www/audio/wrong1.wav');
}
if(quesNo < wordsNo ){
setTimeout(function() {
buttonAns = true;
db.transaction(queryQuesDB, errorCB);
},1800);
}else{
endLevel();
}
} else {
event.preventDefault();
}
});

Android has a finite amount of resources in order to play sounds. You keep creating a new Media object each time you want to play a file. You need to release the sounds you are not using. If you need to play those sounds multiple times each then consider creating separate media objects to hold a reference to those sounds.
Read up on media.release.

I had the same problem.
The solution I did.:
if(media){
media.stop();
media.release();
}
media = new Media(...);

Here's my trick: I released it upon it finished playing or an error occurred.
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);
}
}

Related

Something STRANGE with Cordova media plugin

I have created a sound app for Android using Cordova. Everything was perfect, at least in the Android Studio's emulator, until I found something strange happening while testing on a real device.
My app contains more than 100 sounds one second each. You click to play sounds till you get 32 clicks that play audios without problems. But starting from thirty-third (33rd) click I get a message error:
code: -19 message: undefined
I must mention that you can reply the previous clicked sounds that still work. But other sounds get the error above.
If I close and reopen the app, sounds that gave error work again until I get to 33rd click and start getting the error message even for sounds that used to work.
Here is the code I use to play my sounds:
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
var my_media = new playAudio();
}
// Audio player
//
var my_media = null;
var mediaTimer = null;
// Play audio
//
my_media = {}; // init the play object
function playAudio(src, id) {
if (!my_media[id]) { // check if media already created
my_media[id] = new Media(src, onSuccess, onError);
}
my_media[id].play();
}
//playAudio('file:///android_asset/www/media/1.mp3');
// 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;
}
Any IDEA?
thanks

Audio Play and Pause Ionic Creator app (Audio Streaming - Radio App )

I am developing a radio app using ionic creator.
audio play function works ok, but I need to pause to audio
Another question:
Double clicking to listen to two tunes at once?
this is audio play function,
function ($scope, $stateParams) {
$scope.playWebAudio = function(){
try{
$scope.audio = new Audio('Audio stream URL');
$scope.audio.play();
}
catch(e){
alert(e);
console.log(e);
}
}
play button
ng-click playWebAudio()
Strop Audio you have to use $cordovaMedia plugin instead of $cordovaNativeAudio
now you have to do like this:
module.controller('MyCtrl', function($scope, $cordovaMedia) {
var src = "/src/audio.mp3";
var media = $cordovaMedia.newMedia(src);
var iOSPlayOptions = {
numberOfLoops: 2,
playAudioWhenScreenIsLocked : false
}
media.play(iOSPlayOptions); // iOS only!
media.play(); // Android
media.pause();
media.stop();
media.release();
media.seekTo(5000); // milliseconds value
media.setVolume(0.5);
media.startRecord();
media.stopRecord();
media.getDuration();
media.getCurrentPosition().then(...);
});
for more detail refer to this tlink enter link description here

Media.release() in cordova's Media plugin

How does Media.release() work. Looking at the docs it feels that you have to use it like this:
MediaService.loadMedia('sounds/connection-error.wav').then(function(media){
media.play();
media.release();
});
But I googled enough to know that is wrong. We have to explicitly release the core instances on Android.
But how to do that? If I have 8 views in my app and if I play a sound file on each of those views does that count as 8 core instances being used? And can I go back to say view number 1 and again play the sound associated with that view? If so, would that count as a 9th instances ?
Straight away calling media.release() just like above does not play any sound at all.
Most common way to play sounds using Cordova Media plugin is following:
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);
}
}

actionscript 3 - Play sound once then delay

I am a complete newcomer to as3, having started about 3 months ago as part of a university project, so apologies if I am asking an obvious question or I provide the wrong information.
I am creating a game where a 'monster' from an array collides with a static movie clip which the user has to avoid by killing the monster before they get there.
When the monster reaches the movie clip, I want it to play a 'munch' sound once, then not play it again for a few seconds, but the code as it is means that the sound plays over and over again while the array item and movie clip collide, which does not sound right.
I tried to correct this by adding a timer which allows the sound to continue for half a second and then stops the sound channel, but this does not seem to have worked.
How can I make the sound play once, not play again for the next few seconds and then play again if the objects are still colliding?
Thanks in advance.
Heres my code:
var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));
var muteTest: Boolean = false;
var munchStop:Timer = new Timer (500, 1);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);
function afterMunchStop(event: TimerEvent): void {
sc.stop();
}
if (monster1Array[i].hitTestObject(centre_mc)) {
if (muteTest == false) {
sc = munch.play();
munchStop.start();
}
var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));
var soundIsPlaying: Boolean = false;
//sound should loop until objects no longer collide
var munchStop:Timer = new Timer(500);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
//play sound if monsters collide and sound is not already playing
if (monster1Array[i].hitTestObject(centre_mc) && !soundIsPlaying)
{
sc = munch.play();
soundIsPlaying = true;
//start the timer if it's not running
if(!munchStop.running)
munchStop.start();
} else {
//stop the sound if it's playing
if(sc)
sc.stop();
soundIsPlaying = false;
//reset the timer when the objects are no longer colliding
if(munchStop.running)
munchStop.reset();
}
}
function afterMunchStop(event: TimerEvent): void {
soundIsPlaying = false;
}

phonegap Media Error code 0

first time when i play a mp3 file using phonegap media it works fine. but wen i stop and again play for 2nd time it does not and 3rd time when i play it shows error code 0
below is code
var my_media = null;
var mediaTimer = null;
var pausePos = 0;
var counter=0;
var playing=false;
function playAudio(src) {
// Create Media object from src
if(my_media==null){
my_media = new Media(src, onSuccess, onError);
}
if(!playing){
// get audio duration
var duration = my_media.getDuration();
// set slider data
if( duration > 0 ){
$('#slider').attr( 'max', Math.round(duration) );
$('#slider').slider('refresh');
}
// Play audio
my_media.play();
playing=true;
$("#play_pause_img").attr("src","img/pause.png")
// 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);
}
}
else{
pauseAudio();
playing=false;
$("#play_pause_img").attr("src","img/play.png");
}
}
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
function resumeAudio()
{
my_media.play();
}
function stopAudio() {
if (my_media) {
my_media.stop();
my_media.release();
playing=false;
$("#play_pause_img").attr("src","img/play.png");
}
clearInterval(mediaTimer);
mediaTimer = null;
pausePos = 0;
$('#slider').val(pausePos);
$('#slider').slider('refresh');
}
function onSuccess() {
console.log("playAudio():Audio Success");
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
what could be the reason and what is error code 0.
In 3.5.0 I'm experiencing the error function firing with an error code of 0.
From older docs, 0 appears to mean none, and appears to be no indication of a problem.
I seems under these circumstances this can be ignored, and is not really an error
it has to do with audio-ressources which are finite and therefore must get released. This is explained there: http://docs.phonegap.com/en/3.3.0/cordova_media_media.md.html
In my case it did the trick to call release() for my media object at that moment I don't use it anymore - I do it directly after calling stop()
this.stopAudio = function() {
if (my_media) {
my_media.stop();
my_media.release();
}
}
I have the same problem. I'm searching for the solution, but I didn't find anything.
My code:
$('#button').click(function{
media = new Media('/android_asset/www/sonidos/trans/sound.m4a', play, error);
});
function play(){
media.play();
}
function error(err){
alert(err.code);
}
If i do in that way, the sound does not play and I get always the alert with 0. That not match with any error code from mediaError.
Testing and testing I make code works with next code:
$('#button').click(function{
media = new Media('/android_asset/www/sonidos/trans/sound.m4a', play, error);
media.play();
});
function play(){
;
}
function error(err){
;
}
I know is not the correct way but I can't find other solution.
P.D. Sorry for my bad English.
Add the release method in the onSuccess method in addition to the stop method you created.
function onSuccess() {
my_media.release();
console.log("playAudio():Audio Success");
}

Categories

Resources