簡體   English   中英

MediaPlayer:使用Phaser 2.4.6的Cordova 6.1.1中出現錯誤(1,-2147483648)

[英]MediaPlayer: Error (1,-2147483648) in Cordova 6.1.1 with Phaser 2.4.6

我正在嘗試在使用Cordova 6.1.1和Phaser.io 2.4.6構建的Android游戲中播放音頻文件。 媒體無法在低於API 21左右的Android版本上播放,並顯示錯誤消息

04-21 21:48:57.546 9659-9671/com.jtronlabs.birdu E/MediaPlayer: error (1, -2147483648)
04-21 21:48:57.546 9659-9672/com.jtronlabs.birdu E/MediaPlayer: Error (1,-2147483648)

我已經閱讀了一些答案,但沒有任何幫助。 我使用Phaser的Loader類加載音頻:

this.load.audio('background-music', this.arrayOfCompatibleMusicFileNames('the_plucked_bird') ); 

...

//Phaser has support to load in multiple types of audio formats if the first supplied in the array is not compatible with the browser.
arrayOfCompatibleMusicFileNames: function(key){
  //old versions of android don't play music, they require an absolute pathname (instead of relative). This is a generic solution
  //https://stackoverflow.com/questions/4438822/playing-local-sound-in-phonegap?lq=1
  var path = window.location.pathname;
  path = path.substr( 0, path.lastIndexOf("/")+1 ); //need to remove 'index.html' from the end of pathname
  var aud = path+'assets/audio/';

  //aud = '/android_res/raw/'
  var wav = aud + 'wav/';
  var ogg = aud + 'ogg/';
  var mp3 = aud + 'mp3/';

  console.log(mp3+key+".mp3");

  return [mp3+key+".mp3",ogg+key+".ogg",wav+key+".wav"];
},

在瀏覽器和新版本的Android上均可使用。 在較舊的版本上,我嘗試添加多種格式絕對路徑 ,對$ PROJECT_ROOT / platforms / android / AndroidManifest.xml的外部寫入權限 ,並將文件從/ www移至$ PROJECT_ROOT / platforms / android / res / raw

一無所有。 關於可能出什么問題的任何想法?

編輯:當音頻文件在'res'文件夾中時,我這樣引用它們:

arrayOfCompatibleMusicFileNames: function(key){
  return ['/android_res/raw/'+key+".ogg"];
}

它適用於API 21,但不適用於19或更低版本(就像第一個函數一樣)。

我已經通過使用cordova-plugin-media使音頻工作了。

var path = window.location.pathname;
path = path.substr( 0, path.lastIndexOf("/")+1 ); //need to remove 'index.html' from the end of pathname
var aud = path+'assets/audio/';

var ogg = aud + 'ogg/' + key + ".ogg";

//works!
var snd = new Media(ogg);
snd.play();

但是,我發現這樣做的“正常”方式是導致不良行為的原因。

//does not work... Phaser uses this internally
var snd = new Audio(ogg);
snd.play();

看來我將不得不編寫代碼來測試它是瀏覽器還是Cordova,並分別使用“媒體”或“音頻”。

更新:我編寫了該代碼,它使事情變得混亂,但是可以工作。 如今,我使用Crosswalk ,並且WebAudio可在所有設備上使用。 無需媒體插件和我的代碼中的額外大小寫檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM