Reputation: 1
My music app returns java.io.IOException: Prepare failed.: status=0x1 error after the 8th mp3 track is played or after the same track is played for 8 times. I'm using a standard phonegap media API to run the audio, here's the code:
<!-- language: lang-js -->
// The Audio player
var my_media = null;
var mediaTimer = null;
// duration of media (song)
var dur = -1;
// need to know when paused or not
var is_paused = false;
//Set audio position on page
function setAudioPosition(position) {
$("#audio_position").html(position + " sec");
}
//onSuccess Callback
function onSuccess() {
setAudioPosition(dur);
clearInterval(mediaTimer);
mediaTimer = null;
my_media = null;
is_paused = false;
dur = -1;
}
//onError Callback
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
clearInterval(mediaTimer);
mediaTimer = null;
my_media = null;
is_paused = false;
setAudioPosition("0");
}
function playAudio(src) {
if (my_media === null) {
// ui niceties
$("#media_dur").html("0");
$("#audio_position").html("Loading...");
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// Play audio
//alert('Playing Audio');
my_media.play();
} else {
if (is_paused) {
// to resume where paused in song: call .play()
is_paused = false;
my_media.play();
}
}
// Update my_media position every second
if (mediaTimer === null) {
mediaTimer = setInterval(function() {
my_media.getCurrentPosition(
// success callback
function(position) {
if (position > -1) {
setAudioPosition(Math.round(position));
// getDuration() can take a few seconds so keep trying
// this could be done a better way, no callback for it
if (dur <= 0) {
dur = my_media.getDuration();
if (dur > 0) {
dur = Math.round(dur);
$("#media_dur").html(dur);
}
}
}
},
// error callback
function(e) {
alert("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
//Pause audio
function pauseAudio() {
if (is_paused) return;
if (my_media) {
is_paused = true;
my_media.pause();
}
}
//Stop audio
function stopAudio() {
if (my_media) {
// A successful .stop() will call .release()
my_media.stop();
my_media = null;
}
if (mediaTimer) {
clearInterval(mediaTimer);
mediaTimer = null;
}
is_paused = false;
dur = 0;
}
$('#page-basics').live('pageinit',function(){
$(".pl").live('tap', function() {
var ind = $(".pl").index(this);
var src = "/android_asset/" + ind + ".mp3";
playAudio(src);
});
$("#pauseaudio").live('tap', function() {
pauseAudio();
});
$("#stopaudio").live('tap', function() {
stopAudio();
});
});
Upvotes: 0
Views: 1652
Reputation: 23273
You are running out of OpenCore instances on Android. You need to call my_media.release() to free up an instance of OpenCore so that Android can create a new MediaPlayer. I see in your comments that you say stop calls release but that is not the case. You may want to stop the playing of the audio and start it up again without having to re-create the AudioPlayer.
So what you need to do is once you no longer need the audio anymore you should call:
my_media.release();
Upvotes: 1