Search code examples
javascriptioshtmlcocos2d-jshowler.js

Cocos2D Javascript 3.0 Audio on iOS/iPad


So making a HTML5 game using the new Cocos2D Javascript 3.0 that just came out. Come into a pretty annoying pain in the ass with how audio is played on iOS and iPad. My game is aiming to be cross platform, and the audio works on browser and android devices. It even works on Windows Phone!

I have tried manipulating iOS's event handler (I'm aware that it doesn't allow you to play audio unless the user interacts with the device) but previous versions of cocos2D HTML5 have suggested that it's audio engine used to support iOS with no hassle. Now, it doesn't seem to...

Also I've looked at alternatives such as Howler.js, however implementing that to work with iOS requires that I have SimpleAudioEngine, which I do not believe 3.0 has.

Can anyone recommend any solutions? I'd be happy enough if I was even able to just play the background music on iOS.

I was using this to help for anyone interested http://www.cocos2d-x.org/docs/manual/framework/html5/release-notes/v3.0a/upgrade-guide/en


Solution

  • Fixed it. As I said, you can only play sounds once the user has actually interacted with the system, via a touch or click etc... I had this prior to asking my question. Seems the problem was that the audio hadn't loaded in time for the event to be triggered :D

    Changed my audio to a .mp3 since it's much smaller, and wrapped my music to be played upon button entry.

    var closeItem = cc.MenuItemSprite.create(
                cc.Sprite.create(res.play),
                playButton,
                function () {
                    cc.log("New Game!");
                    this.playEffect();
    );
    
    
    playEffect:function() {
        var audioengine = cc.audioEngine;
        audioengine.playMusic(res.music, true);
    }