Android PhoneGap 音频应用程序返回 java.io.IOException: 准备失败。: status=0x1 错误
我的音乐应用程序在播放第 8 首 mp3 曲目或播放同一曲目 8 次后返回 java.io.IOException: Preparation failed.: status=0x1 错误。 我使用标准的phonegap媒体API来运行音频,代码如下:
<!-- 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();
});
});
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();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Android 上的 OpenCore 实例已用完。您需要调用 my_media.release() 来释放 OpenCore 实例,以便 Android 可以创建新的 MediaPlayer。我在您的评论中看到您说停止调用释放,但事实并非如此。您可能想要停止播放音频并再次启动它,而不必重新创建 AudioPlayer。
因此,您需要做的是,一旦您不再需要音频,您应该调用:
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: