我正在使用phonegap创建一个RSS播放器应用。一切都与暂停/恢复功能分开。暂停很好,但再次按下暂停按钮(恢复播放)时,它会回到mp3的开头。我的代码如下。任何帮助都会非常感激。
function onDeviceReady() {
//window.alert("Loading PhoneGap is completed");
}
var media = null;
var mediaTimer = null;
function playAudio() {
//get url passed from previous screen.
var mp3 = localStorage.getItem("url_to_play");
var src = mp3;
media = new Media (src , onSuccess, onError);
//Play the audio. You can set number of the replaying time here.
media.play({numberOfLoops:"infinite"});
if (mediaTimer == null) {
mediaTimer = setInterval(function() {
// Return a current playback position
media.getCurrentPosition(
//A Callback function if it's success
function(position) {
if (position > -1) {
setAudioPosition((position) + " sec");
//If the playback stops at "-0.001" position, replay the audio.
if(position == -0.001){
media.play({numberOfLoops:"infinite"});
}
}
},
//A callback function in case of failure
function(e) {
console.log("Error getting pos=" + e);
setAudioPosition("Error: " + e);
}
);
}, 1000);
}
}
function pauseAudio() {
if (media) {
media.pause();
}
}
function stopAudio() {
if (media) {
media.stop();
}
clearInterval(mediaTimer);
mediaTimer = null;
}
答案 0 :(得分:2)
我通过执行以下操作对此进行了排序:
if(media === null){media = new Media (src , onSuccess, onError);}
仅在新媒体对象不存在时才创建它。如果它确实存在,那么它已经在播放并且不需要它。