从 Phaser v2.0.1 升级到 v2.7.7 会破坏声音

Upgrading from Phaser v2.0.1 to v2.7.7 breaks sound

我刚刚在我的 Phaser 应用程序中用最新版本 (2.7.7) 替换了缩小的 Phaser js,我的应用程序中的音频已停止工作。该应用程序的其余部分仍然运行良好。加载音频的代码非常简单 - 我首先加载音频,然后将音频添加到数组中。

game.load.audio('sound.correct', 'assets/sound.general/sound.correct.mp3');

general_sounds[0] = game.add.audio('sound.correct');

当我开始播放音频时,我只使用:

general_sounds[0].play()

它在 2.0.1 中有效,但现在我得到的错误如下:

phaser.min.js:3 Uncaught TypeError: Cannot read property 'createBufferSource' of null
    at c.Sound.play (phaser.min.js:3)
    at <anonymous>:1:19
play    @   phaser.min.js:3
(anonymous) @   VM1834:1

更奇怪的是,当我在控制台中 运行 以上 3 个命令时,声音一切正常。

有人知道这是怎么回事吗?

编辑:下面是我正在使用的代码的简化版本 - 在下面,general_sounds[1] 播放但 0 和 2 不播放。如果我将 loadGeneralSound(); 调用放在 create(){} 而不是 preload(){} 中,我会在代码下方收到错误。

<!DOCTYPE html>
<html lang="en"> 
<head> 
    <meta charset="UTF-8" />
    <title>Snap - it's kids play!</title>
    <script type="text/javascript" src="js/phaser.min.js"></script>
    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>
<body>

<script type="text/javascript">

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create});
var general_sounds = [];

function preload() {
    // load general sounds
    loadGeneralSound(); 
}

function create() {
    general_sounds[0].play();
    general_sounds[1].play();
    general_sounds[2].play();

}

// loads general sounds that are not object specific
function loadGeneralSound() {
    game.load.audio('sound.correct', 'assets/sound.general/sound.correct.mp3');
    game.load.audio('sound.incorrect', 'assets/sound.general/sound.incorrect.mp3');
    game.load.audio('voice.correct', 'assets/sound.general/sound.well_done.m4a');

    general_sounds[0] = game.add.audio('sound.correct');
    general_sounds[1] = game.add.audio('sound.incorrect');
    general_sounds[2] = game.add.audio('voice.correct');

}

</script>

</body>
</html>

如果我将 loadGeneralSound(); 调用放在 create(){} 而不是 preload(){} 中,则会出错:

   Phaser CE v2.7.7 | Pixi.js | WebGL | WebAudio     http://phaser.io ♥♥♥
phaser.min.js:3 Phaser.Cache.isSoundDecoded: Key "sound.correct" not found in Cache.
getItem @ phaser.min.js:3
isSoundDecoded @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:27
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3
phaser.min.js:3 Phaser.Cache.getSound: Key "sound.correct" not found in Cache.
getItem @ phaser.min.js:3
getSound @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:27
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3
phaser.min.js:3 Phaser.Cache.isSoundDecoded: Key "sound.incorrect" not found in Cache.
getItem @ phaser.min.js:3
isSoundDecoded @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:28
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3
phaser.min.js:3 Phaser.Cache.getSound: Key "sound.incorrect" not found in Cache.
getItem @ phaser.min.js:3
getSound @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:28
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3
phaser.min.js:3 Phaser.Cache.isSoundDecoded: Key "voice.correct" not found in Cache.
getItem @ phaser.min.js:3
isSoundDecoded @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:29
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3
phaser.min.js:3 Phaser.Cache.getSound: Key "voice.correct" not found in Cache.
getItem @ phaser.min.js:3
getSound @ phaser.min.js:3
play @ phaser.min.js:3
create @ snap.html:29
loadComplete @ phaser.min.js:3
preUpdate @ phaser.min.js:3
updateLogic @ phaser.min.js:3
update @ phaser.min.js:3
updateRAF @ phaser.min.js:3
window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser.min.js:3

Interphase 1 关于状态管理器的文档声明所有 game.load 调用都应该在 preload 中发生,以确保它们可用。有趣的是,我无法将您的示例提供给 运行,但如果我将加载调用移至 create.

,我可以重现该问题

official Play Music example 进行了拆分,因此 game.load 发生在 preload 中,game.add.audio 发生在 create 中。更改您的代码以遵循该标准对我有用。

例如:

function preload() {
    // load general sounds into cache
    loadGeneralSound(); 
}

function create() {
    // Add audio to the array so they can be played.
    addGeneralSound();
    general_sounds[0].play();
    general_sounds[1].play();
    general_sounds[2].play();
}

// loads general sounds that are not object specific
function loadGeneralSound() {
    game.load.audio('sound.correct', 'assets/sound.general/sound.correct.mp3');
    game.load.audio('sound.incorrect', 'assets/sound.general/sound.incorrect.mp3');
    game.load.audio('voice.correct', 'assets/sound.general/sound.well_done.m4a');
}

// Add sounds to the array object so they can be referenced/played.
function addGeneralSound() {
    general_sounds[0] = game.add.audio('sound.correct');
    general_sounds[1] = game.add.audio('sound.incorrect');
    general_sounds[2] = game.add.audio('voice.correct');
}