2012-12-05 37 views
3

我想開發一種帶有PhoneGap(Cordova 2.2.0)的媒體播放器,並將Adobe的Phonegap Build用於其他平臺。這意味着幾乎所有東西都必須是相對的;)我的應用程序的開發是在Android 2.3(最小目標)上進行的。PhoneGap中的相對媒體源

這裏是我的問題:

到文件的路徑效果很好,當它從網上(http://...)的,或已經給出了明確的路徑如下(/android_asset/www/media/song.mp3)所示。然而,試圖做到這一點時,相對(media/song.mp3)這首歌不會開始播放,給我一個錯誤代碼:

的MediaPlayer:錯誤(1,-2147483648)

如果我按下播放鍵我又得到了幾個錯誤:

的MediaPlayer:啓動名爲狀態0 //意思是沒有文件
的MediaPlayer:錯誤(-38,0)
MediaPlayer的錯誤:(-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); 
    } 
}; 

如果有什麼多說的是必要的,請告訴我。

回答

8

在所有運行PhoneGap的操作系統上,Media API未正確規範化。如果您想從Android的資產文件夾中播放某些內容,則需要指定完整路徑。使用device.name屬性來檢測您是否在Android上並預先設置路徑爲/android_asset/www/

+0

權,但對於其他平臺?如果我不會在我的'www'文件夾中添加特定於平臺的路徑,媒體是否可見? – Cornelius

+0

嗯,@Simon你能回答嗎? – Cornelius

+2

這裏有一種方法可以解決它http://simonmacdonald.blogspot.ca/2012/01/on-eleventh-day-of-phonegapping.html –

0
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() 
} 

}

+0

Wroks對我很好! – Adimz