下面是我现在写的代码
var my_media = null;
var mediaTimer = null;
var pausePos = 0;
var counter=0;
var playing=false;
function playAudio(src) {
// Create Media object from src
my_media = new Media(src, onSuccess, onError);
// Play audio
my_media.play();
// get audio duration
var duration = my_media.getDuration();
// set slider data
if( duration > 0 ){
$('#slider').attr( 'max', Math.round(duration) );
$('#slider').slider('refresh');
}
// 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);
}
}
/* pause audio */
function pauseAudio() {
if (my_media) {
my_media.pause();
}
}
function resumeAudio()
{
}
/* stop audio */
function stopAudio() {
if (my_media) {
my_media.stop();
playing = false;
my_media.release();
}
clearInterval(mediaTimer);
mediaTimer = null;
pausePos = 0;
}
// onSuccess Callback
function onSuccess() {
console.log("playAudio():Audio Success");
}
// onError Callback
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
// Set audio position
function setAudioPosition(position) {
pausePos = position;
position = Math.round(position);
$('#slider').val(position);
$('#slider').slider('refresh');
}
function player(id)
{
//alert(id);
playAudio("/sdcard/MuseumCache/"+id+"/"+id+".mp3");
//alert("end");
}
请帮我写简历功能。他们说media.play()播放并恢复播放音频文件
答案 0 :(得分:1)
好吧我喜欢这个解决方案:Phonegap的Docs非常糟糕......很难解释 所以他们的例子很草率
重点是:每次玩游戏时都不得重新初始化媒体,否则从头开始
var my_media == null;
function playAudio(url) {
if (my_media === null){
// so only is your media handler is empty you initialiaze it otherwise you just play
my_media = new Media(url, function() {
console.log("Audio Success");
}, function(err) {
console.log("Audio Error");
});
}
my_media.play();
}
答案 1 :(得分:0)
答案 2 :(得分:0)
Francesco你的解决方案只有在你有一个声音可以播放的情况下工作,如果你在同一页面上有多个声音,每个玩家将获得第一个媒体的属性...