Android phonegap音频应用返回java.io.IOException:准备失败:status = 0x1错误

时间:2012-02-06 19:50:26

标签: android cordova

我的音乐应用程序返回java.io.IOException:准备失败:播放第8首mp3曲目后或在同一曲目播放8次后出现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();
});
});

1 个答案:

答案 0 :(得分:1)

Android上的OpenCore实例已用完。您需要调用my_media.release()来释放OpenCore实例,以便Android可以创建新的MediaPlayer。我在你的评论中看到你说停止呼叫释放但事实并非如此。您可能希望停止播放音频并重新启动它,而无需重新创建AudioPlayer。

所以你需要做的就是你不再需要音频了,你应该打电话:

my_media.release();