最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Looping audio in Phaser - Stack Overflow

programmeradmin4浏览0评论

I've just started making a simple platformer with Phaser, I'm new to this.

I have managed to get music playing but cant get it to loop and Google hasn't helped.

Heres the audio code I'm using, any advice?

game.load.audio('hotttt', ['assets/audio/hotttt.mp3', 'assets/audio/hotttt.ogg']);

music = game.add.audio('hotttt');

music.play();

I've just started making a simple platformer with Phaser, I'm new to this.

I have managed to get music playing but cant get it to loop and Google hasn't helped.

Heres the audio code I'm using, any advice?

game.load.audio('hotttt', ['assets/audio/hotttt.mp3', 'assets/audio/hotttt.ogg']);

music = game.add.audio('hotttt');

music.play();
Share Improve this question edited Dec 11, 2015 at 16:38 Apovtx 3052 silver badges14 bronze badges asked Dec 10, 2015 at 19:41 Ger McGer Mc 6403 gold badges11 silver badges23 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 10

First, you load the audio file like you did, and then create the instance for the audio itselfs.

Example:

game.load.audio('background_music', ['assets/sounds/background_music.mp3', 'assets/sounds/background_music.wav']);

backgroundMusic = game.add.audio('background_music');
backgroundMusic.loop = true; // This is what you are looking for
backgroundMusic.play();

Hope it helps!

You will need to create a new Phaser.Sound object and enable loop.

game.load.audio('hotttt', ['assets/audio/hotttt.mp3', 'assets/audio/hotttt.ogg']);

// *true* param enables looping
music = new Phaser.Sound(game,'hotttt',1,true);

music.play();

You can refer to the documentation - Phaser.Sound

According to the docs for v2.4.4 (and I tested it with 2.6.1) you can pass an extra argument to the game.add.audio() function for looping

Example

game.load.audio('background', ['assets/audio/background.wav'])

const backgroundSound = game.add.audio('background', 0.5, true) // here "true" means to loop

backgroundSound.play()

That worked fine for me

The best solution that works for me is this:

music = game.add.audio('yourMusicFile');
music.loopFull()

Anyone who is looking at this thread for Phaser 3.

Load music in preload()

this.load.audio('musicaudio', 'assets/musicl.mp3');

Then in your own function or create()

var music = this.sound.add('musicaudio');
music.setLoop(true);
music.play();

I don't quite understand yet the mechanics behind initializing audio but in order do so in my Chrome environment I had to set this in the initial config declaration.

audio: {
        disableWebAudio: true,
        noAudio: false
    },

P.S. I read somewhere that you shouldn't use mp3's for looping but it works fine for me. Perhaps there is a brief pause.

发布评论

评论列表(0)

  1. 暂无评论