PhoneGap中的相对媒体来源



我想用PhoneGap(Cordova 2.2.0)开发一种媒体播放器,并在其他平台上使用Adobe的PhoneGap Build。这意味着几乎所有的东西都必须是相对的;)我的应用程序是在Android 2.3上开发的(最低目标)。

这是我的问题:

当文件的路径来自网络(http://...),或者已经被指定了如下所示的确定路径(/android_asset/www/media/song.mp3)时,文件的路径运行良好。然而,当尝试相对地(media/song.mp3)播放时,歌曲不会开始播放,给了我一个错误代码:

MediaPlayer:错误(1,-2147483648)

如果我再次按下播放键,我会收到更多错误:

MediaPlayer:在状态0//下调用开始,这意味着没有文件
MediaPlayer:error(-38,0)
MediaPlayer:error(-38,0)

有没有一种方法可以让它相对工作,而不需要:

if(Android){
src=...
}
if(iOS){
src=...;
}...

上面的选项很耗时,如果其中一个平台的路径发生更改,则会变得容易出错。

这是缩小的Javascript代码:

var song;
var app = {
initialize : function() {
this.bindEvents();
},
bindEvents : function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady : function() {
app.receivedEvent('deviceready');
song = new Media("/android_asset/www/media/song.mp3");
console.log("The song is: " + (song.mediaStatus!=0?"okay":"none"));
$('#play').click(function() {
song.play();
console.log("Song started");
});
$('#pause').click(function() {
song.pause();
console.log("Song paused");
});
$('#stop').click(function() {
song.stop();
console.log("Song stopped");
});
},
receivedEvent : function(id) {
console.log('Received Event: ' + id);
}
};

如果还有什么需要说的,请告诉我。

媒体API没有在PhoneGap运行的所有操作系统中正确规范化。如果你想在Android上播放assets文件夹中的内容,你需要指定完整路径。你最好使用device.name属性来检测你是否在Android上,并准备/Android_asset/www/路径。

var media = {
basePath: '',
sounds: [],
init: function(){
if(device != undefined && device.platform == 'Android')
{
this.basePath = '/android_asset/www';
}
this.sounds['eggTap'] = new Media(this.basePath+'/sounds/click.mp3')
},
play: function(target){
this.sounds[target].stop()
this.sounds[target].play()
}

}

最新更新