首页 > 解决方案 > 连续播放 mp3 声音样本时移相器警告“音频源已存在”

问题描述

我对加载和播放音效有点困惑。我的游戏设置在不同的状态,首先 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.
};

问题是,当我每隔几秒钟连续播放几次击打声时,控制台会发出此警告(Phaser在此处的代码中提出)

Phaser.Sound:音频源已经存在

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

我怀疑它与解码 mp3 和 ogg声音有关。每次玩家开始(或重新开始)关卡(即重新启动 GameState)时,我是否必须解码声音样本?换句话说,如果 GameState 将在.create()每次(重新)启动关卡并添加音频样本时使用game.add.audio,那么来自上一个关卡的解码样本是否会被破坏并且每次都必须重新加载/解码?这似乎很浪费,最好的方法是什么?所以我的问题是:

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

标签: javascriptaudiophaser-framework

解决方案


好吧,据我所知,您的代码似乎做对了。因此,我将尝试用我所拥有的知识来回答您的问题:

1. 这个消息“音频源已经存在”是什么意思?还是我应该忽略它?

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

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

        // this._disconnectSource();
    }

如果您尝试播放的声音已经由 Phaser.Sound 播放,并且如果不是 allowMultiple... 则它会引发此错误,这就是问题的交换。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;

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

问题 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;

推荐阅读