当连续播放mp3声音样本时,Phaser警告"音频源已经存在"

BdR*_*BdR 5 javascript audio phaser-framework

我对加载和播放音效有点困惑.我的游戏设置在不同的状态,首先Preloader状态确保加载所有图像和声音.GameState是主要游戏,每个下一个级别重新启动此状态.有不同的级别,但状态是相同的,它只是更改一个_levelIndex变量并使用相同的状态.

GameState在.create()函数中为游戏添加所需的音频,每次启动GameState时都会调用此create-function.见下面的代码

mygame.Preloader.prototype = {
    preload: function(){

        this.loadingbar_bg = this.add.sprite(80, 512, "loadingbar_bg");
        this.loadingbar_fill = this.add.sprite(80, 512, "loadingbar_fill");
        this.load.setPreloadSprite(this.loadingbar_fill);

        // load sounds
        this.load.audio("button", ["snd/button.mp3", "snd/button.ogg"]);
        this.load.audio("punch",  ["snd/punch.mp3",  "snd/punch.ogg"]);
        this.load.audio("coin",   ["snd/coin.mp3",   "snd/coin.ogg"]);
    },
    create: function() {
        this.state.start("MainGame");
    },
};

mygame.GameState.prototype = {

    create: function() {
        this.stage.backgroundColor = "#f0f";
        // etc.

        // sound effects
        this.sound1 = this.game.add.audio("button");
        this.sound2 = this.game.add.audio("punch");
        this.sound3 = this.game.add.audio("coin");
        //etc.
    },

    update: function() {
        if (hitFace) {
            this.sound2.play();
            hitFace = false;
        };
    },

    doNextLevel: function() {
        this.sound1.play();
        this._levelIndex++; // next level
        this.state.start("MainGame"); // restart this state
    },
    //etc.
};
Run Code Online (Sandbox Code Playgroud)

问题是,当我连续几次连续播放一次打孔声时,控制台会发出此警告(Phaser 在这里提出了代码)

Phaser.Sound:音频源已经存在

即使第一次启动GameState,也会出现此警告.

我怀疑它与解码mp3和ogg声音有关.每次播放器启动(或重启)一个级别即重启GameState时,是否必须解码声音样本?换句话说,如果GameState将在.create()每次(重新)开始并且使用的情况下添加音频样本时game.add.audio,是否会破坏前一级别的解码样本并且每次都必须重新加载/解码?这看起来很浪费,最好的办法是什么?所以我的问题是:

  1. 这条消息"音频源已经存在"是什么意思?或者我应该忽略它?
  2. 如果我想在状态中使用声音,每次启动状态并.create()调用时我是否必须重新添加它们?
  3. 也有点相关,如果我想在多个不同的状态(菜单,游戏,选项等)使用相同的声音样本我是否必须game.add.audio()为每个州的相同声音做?

Sir*_*ple 2

好吧,据我所知,您的代码似乎做得正确。所以我会尝试用我所掌握的知识来回答你的问题:

1. “音频源已存在”是什么意思?或者我应该忽略它?

该消息意味着已经有一个该声音的实例正在播放,正如您在发出声音的地方看到的那样:

if (this._sound && ***!this.allowMultiple***)
    {
        console.warn('Phaser.Sound: Audio source already exists');

        // this._disconnectSource();
    }
Run Code Online (Sandbox Code Playgroud)

如果您尝试播放的声音已经由 Phaser.Sound 播放,并且如果不允许,则会抛出此错误...这就是问题的关键。源代码中的AllowMultiple :

/**
* @property {boolean} allowMultiple - This will allow you to have multiple instances of this Sound playing at once. This is only useful when running under Web Audio, and we recommend you implement a local pooling system to not flood the sound channels.
* @default
*/
this.allowMultiple = false;
Run Code Online (Sandbox Code Playgroud)

所以基本上是抱怨你试图产生多次不允许的声音的多个实例。您不应该忽略它,而应该使用正确的标志。

问题2和3:

您不应该重新添加资源,因为这就是您在引擎中加载音频源的原因,以便可以在所有级别中重用。您也不必为所有州都这样做。

为了在多种状态下重用声音,您应该能够在全局范围内添加音频或任何游戏对象并访问它(在这里我发现有人试图做您在问题中提出的问题)其他方法是添加将此资源作为游戏对象的属性,因此您不会污染全局范围,而只会污染游戏对象上下文。但我相信在不同的州添加此音频并管理它们在各州的删除/创建是更好的策略。主要是因为 JS 是邪恶的*,可变性可能会对你产生不利影响

*没那么邪恶

要解决此警告:只需使用标志allowMultiple在此处创建),例如:

    this.sound1 = this.game.add.audio("button") // allowMultiple is false by default
    this.sound2 = this.game.add.audio("punch");
    // Allow multiple instances running at the same time for sound2
    this.sound2.allowMultiple = true;
    this.sound3 = this.game.add.audio("coin");
    // Allow multiple instances running at the same time for sound3
    this.sound3.allowMultiple = true;
Run Code Online (Sandbox Code Playgroud)