连续播放 mp3 声音样本时 Phaser 警告 "Audio source already exists"
Phaser warning "Audio source already exists" when mp3 sound sample is played consecutively
我对加载和播放音效有点困惑。我的游戏设置为不同的状态,首先预加载器状态确保所有图像和声音都已加载。 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()
,那么上一关卡的解码样本是否会被破坏并且必须reloaded/decoded 每次?这似乎很浪费,最好的方法是什么?所以我的问题是:
- 这条消息 "Audio source already exists" 是什么意思?要么
我应该忽略它吗?
- 如果我想在某个状态下使用声音,我是否必须在每次启动该状态并调用
.create()
时重新添加它们?
- 也有点相关,如果我想在多个不同的状态(菜单、游戏、选项等)中使用相同的声音样本,我是否必须为每个状态使用相同的声音
game.add.audio()
?
好吧,据我所知,您的代码似乎做对了。所以我会尽量用我所知道的来回答你的问题:
1.此消息 "Audio source already exists" 是什么意思?还是我应该忽略它?
该消息意味着已经有一个该声音正在播放的实例,您可以在它发出的地方看到:
if (this._sound && ***!this.allowMultiple***)
{
console.warn('Phaser.Sound: Audio source already exists');
// this._disconnectSource();
}
如果您尝试播放的声音已经被 Phaser.Sound 播放,并且如果不是 allowMultiple...这就是问题所在。 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:
你不应该重新添加资源,因为这就是你在引擎中加载音频源的原因,以便可以在所有级别中重复使用。您也不必为所有州都这样做。
为了在多个状态下重用声音,您应该能够在全局范围内添加音频或任何游戏对象并访问它 ()
其他方法是将此资源作为属性添加到游戏对象,这样您就不会污染全局范围,而只会污染游戏对象上下文。
但我认为 在不同州添加此音频 并在各州管理其 deletion/creation 是更好的策略。主要是因为 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;
我对加载和播放音效有点困惑。我的游戏设置为不同的状态,首先预加载器状态确保所有图像和声音都已加载。 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()
,那么上一关卡的解码样本是否会被破坏并且必须reloaded/decoded 每次?这似乎很浪费,最好的方法是什么?所以我的问题是:
- 这条消息 "Audio source already exists" 是什么意思?要么 我应该忽略它吗?
- 如果我想在某个状态下使用声音,我是否必须在每次启动该状态并调用
.create()
时重新添加它们? - 也有点相关,如果我想在多个不同的状态(菜单、游戏、选项等)中使用相同的声音样本,我是否必须为每个状态使用相同的声音
game.add.audio()
?
好吧,据我所知,您的代码似乎做对了。所以我会尽量用我所知道的来回答你的问题:
1.此消息 "Audio source already exists" 是什么意思?还是我应该忽略它?
该消息意味着已经有一个该声音正在播放的实例,您可以在它发出的地方看到:
if (this._sound && ***!this.allowMultiple***)
{
console.warn('Phaser.Sound: Audio source already exists');
// this._disconnectSource();
}
如果您尝试播放的声音已经被 Phaser.Sound 播放,并且如果不是 allowMultiple...这就是问题所在。 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:
你不应该重新添加资源,因为这就是你在引擎中加载音频源的原因,以便可以在所有级别中重复使用。您也不必为所有州都这样做。
为了在多个状态下重用声音,您应该能够在全局范围内添加音频或任何游戏对象并访问它 (
*没那么邪恶
要解决此警告:只需使用标志 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;