Phonegap Media Error Code 0

The first time I play an mp3 file using phonegap media, it works great. but wen I stop and play again the 2nd time when it is not, and the third time I play it, error code 0 appears

below is the 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 causing and what is error code 0.

+4
source share
4 answers

it relates to audio resources that are finite and therefore need to be released. This is explained there: http://docs.phonegap.com/en/3.3.0/cordova_media_media.md.html

In my case, he did the trick to call release () for my media object at the moment when I no longer use it - I do this immediately after calling stop ()

  this.stopAudio = function() { if (my_media) { my_media.stop(); my_media.release(); } } 
+2
source

In 3.5.0 I ran into running an error function with error code 0 .

From old documents, 0 means none and does not seem to indicate a problem.

It seems to me that in these circumstances this can be ignored and is not really a mistake

+1
source

I have the same problem. I am looking for a solution but haven’t found anything.

My code is:

 $('#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 this, the sound does not play, and I always get a warning with 0. This does not match the error code from mediaError.

Testing and testing I'm doing code works with the following 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 this is not the right way, but I cannot find another solution.

PD Sorry for my bad english.

0
source

Add the release method to the onSuccess method in addition to the stop method you created.

 function onSuccess() { my_media.release(); console.log("playAudio():Audio Success"); } 
0
source

Source: https://habr.com/ru/post/1443625/


All Articles