我创建了一个简单的Phaser 3测试应用程序(在 Typescript 中,使用 rollup
进行转译)并使用 Capacitor将其转换为我的 Mac 上的 iOS 应用程序。
这是应用程序的相关部分:
function preload () {
this.load.audio('boom', ['boom.mp3', 'boom.ogg', './boom-44100.ogg', './boom-44100.mp3']);
this.load.image('wizball', './wizball.png');
}
function create () {
const image = this.add.image(400, 300, 'wizball').setInteractive();;
this.boom = this.sound.add('boom');
image.on('pointerup', () => {
this.boom.play();
});
}
该应用程序显示 wizball 的图像,如果单击它,您会听到“轰隆”声。
当我运行它时:
- 使用
npm run watch
,在 Mac 上的浏览器中使用 http://localhost:10001,效果很好; - 通过在 Mac 上的浏览器中的
dist/
目录中加载index.html
,它可以正常工作; - 通过加载https://garion.org/soundtest-ts/在我的 Mac 或 iPad 上,它都运行良好;
- 但是当我使用 Capacitor 在 Xcode 中构建 iOS 应用程序时,单击图像根本没有声音。
以下是生成 iOS 应用程序的步骤:
npm i
npm run watch
npx cap add ios
npx cap copy ios
npx cap open ios
Xcode 中的控制台日志显示以下错误:
Error: There is no audio asset with key "boom" in the audio cache
⚡️ URL: capacitor://localhost/game.js
我觉得这很奇怪,因为可以很好地找到图像资源。在 ios/App/public/
目录中,boom.mp3
和 wizball.png
都存在。
我已将完整的代码以及重现步骤放在此处:https://github.com/joostvunderink/soundtest您需要安装 Node 10+ 和 Xcode(至少配置一个虚拟设备)来构建 iOS 应用程序。
我忽略了什么?
最佳答案
在游戏配置中禁用网络音频,添加到游戏配置的底部,如下所示。
let game = new Phaser.Game({
...
audio: {
disableWebAudio: true
}
});
警告:
- 禁用网络音频将使 Phaser 使用 html5 音频。
- 使用 html5 音频 而不是网络音频会让你的游戏滞后
此问题的另一个解决方法是:
- 使用外部音频文件,如果您使用网络音频仍然可以工作 音频文件不是来自内部资源(我仍然找不到原因)
- 使用 native 音频/媒体插件为移相电容器应用播放音频
关于javascript - 使用 Capacitor 为 iOS 构建时,Phaser 3 应用程序没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63864590/