Adding background music

Adding background music

This has probably been answered before but I have successfully made my first game using cocos, a breakout clone. I am wondering how I can have a song looping in the background.

Thanks in advance, bocces

You may play your song in loop mode.

cc.AudioEngine.getInstance().playMusic(s_bgMusic, true);  // The second parameter is the optional of whether the music loop or not.

Hi, this works great but the music dont work on firefox, its mp3 file. its has to do some validations or something for the format? which one should i use?

It is better to keep both .mp3 and .ogg files at the same time to support multi browsers.

The file type will be selected automatically during resources preloading.

Thanks, all this works perfect,

I ran into a problem on the ipad Safari, the music dont play, any advices here,
i have this working on pc (crome,safari, IE, firefox) just fine, testing on the device, the audio
dont work.

I’m not sure if this is a new problem to iOS6, but there are various reports on the web about audio being initially muted in iOS in the Web Audio API, see, but I’m not sure if something has changed in iOS6 or whether this has always been the same.

The MoonWarriors example doesnt play any music on an iPad running iOS6. The only sound that is played is the buttonEffet.mp3, - and I suspect this is played because its response to user input.

BTW. on Mac (Lion), MoonWarriors works fine both in Safari and Firefox.

So this looks like a “mobile” safari issue.

BTW. I’ve also noticed that despite putting my sound resources into the resources.js to preload, the sounds do not appear to be being preloaded. So that the first time they are played they take a long time before any sound, but subsequent calls to playEffect make the sound play much more quickly.


You may also want to read this article I just found
and this