javascript - 连续播放 mp3 声音样本时 Phaser 警告 "Audio source already exists"

标签 javascript audio phaser-framework

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

GameState 在 .create() 函数中将所需的音频添加到游戏中,每次 GameState 启动时都会调用此创建函数。看下面的代码

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 引发 in code here)

Phaser.Sound: Audio source already exists

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

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

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

最佳答案

好吧,据我所知,您的代码似乎做事正确。所以我会尽量用我所知道的来回答你的问题:

<强>1。这条消息“音频源已经存在”是什么意思?还是我应该忽略它?

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

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

        // this._disconnectSource();
    }

如果您尝试播放的声音已经由 Phaser.Sound 播放,并且如果不是 allowMultiple,则会抛出此错误...这就是问题所在。允许多个来自 source code :

/**
* @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:

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

为了在多个状态下重用声音,您应该能够在全局范围内添加音频或任何游戏对象并访问它(Here I found someone trying to do what you ask in the question) 其他方法是将此资源作为属性添加到游戏对象,这样您就不会污染全局范围,而只会污染游戏对象上下文。 但我认为,在不同状态下添加此音频 并在各州管理它们的删除/创建是更好的策略。主要是因为 JS 是邪恶的*,可变性可能会对你不利

*没那么邪恶

要解决此警告:只需使用标志 allowMultiple ( created in here ),例如:

    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;

关于javascript - 连续播放 mp3 声音样本时 Phaser 警告 "Audio source already exists",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857942/

相关文章:

javascript - 如何让我的弹出图像继续显示?

unity3d - 在运行时从 persistentDataPath 加载音频(Unity3d WebGL)

audio - 这2个wav文件有什么区别?

javascript - 在 Phaser 3 中暂停和恢复游戏

javascript - 移相器 3 : destroying all instances of a sprite

javascript - 获取父项的高度并使用 jQuery 分配给子项

javascript - 在滚动 Angular 调整标题大小

javascript - Grails:AJAX onSuccess方法未触发

Java 声音可视化器

npm - 对 TypeScript 使用 "require(...)"的方式感到困惑