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)
我已经阅读了一些 SO 答案,但没有任何帮助。我使用 Phaser 的加载器加载音频 class:
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
//
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。在旧版本上,我尝试添加多个 formats, the absolute path, external write permissions to $PROJECT_ROOT/platforms/android/AndroidManifest.xml, and moving the files from /www to $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();
但是,我发现 'normal' 这样做的方式是导致不良行为的原因。
//does not work... Phaser uses this internally
var snd = new Audio(ogg);
snd.play();
看来要写代码测试是browser还是cordova,分别用'Media'或'Audio'
更新:我写了那个代码,它让事情变得混乱,但有效。现在我使用 Crosswalk,并且 WebAudio 可以在所有设备上运行。我的代码中不需要媒体插件和额外的大小写检查。
我正在尝试在使用 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)
我已经阅读了一些 SO 答案,但没有任何帮助。我使用 Phaser 的加载器加载音频 class:
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
//
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。在旧版本上,我尝试添加多个 formats, the absolute path, external write permissions to $PROJECT_ROOT/platforms/android/AndroidManifest.xml, and moving the files from /www to $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();
但是,我发现 'normal' 这样做的方式是导致不良行为的原因。
//does not work... Phaser uses this internally
var snd = new Audio(ogg);
snd.play();
看来要写代码测试是browser还是cordova,分别用'Media'或'Audio'
更新:我写了那个代码,它让事情变得混乱,但有效。现在我使用 Crosswalk,并且 WebAudio 可以在所有设备上运行。我的代码中不需要媒体插件和额外的大小写检查。