Can't play audio on device using cc.AudioEngine.getinstance().playEffct()

my game can play audio perfectly on PC browser, but can’t play on device browser. Please help!

Yes, we do have problems of audio play with mobile devices… Like on all iOS versions and some Android(e.g. sumsung)

On many device, the problem is that the system doesn’t even load the music if the play audio code isn’t in some user interactions callback processus.
My advise is that play your audio always on events, like when user touches a button to enter a scene, in the touch event callback function, you start to play the music for the next scene. Otherwise if you put play audio code in the scene initialization function, the audio will never get played on many mobile device.
Note that, when I say the event callback processus, it doesn’t mean exactly the callback function, your direct callback function to the event may call another function to switch the scene. In this case, you can just play the music in the last function which switch the scene. That’s in the callback processus.

Huabin

Hi,

There is an issue I have just found. I have been following the audio issues in Cocos2d-html5 very carefully and doing many tests over the past 7 to 9 months. The recent change of the file CocosDenshion/SimpleAudioEngine.js from version 2.2 to 2.2.1 has broken the ability for audio to play back in iOS in my tests. I run the exact same code with 2.2 and I will hear audio on iOS. When I use 2.2.1 version, the audio is gone from iOS. I am hearing fine on Android on both, so no issue there.

So this means that something was changed incorrectly on the recent SimpleAudioEngine upgrade. There were many lines of code changed in that recent version, it was changed a good amount, so I’m unsure where the issue lies in that file.

Please let me know if I can assist with finding the core of the problem and if you need me to show examples. For now I believe I will try using the older 2.2 version of CocosDenshion
SimpleAudioEngine.js.

Please note to anyone just beginning with cocos2d-html5 — html5 audio on mobile devices will only play with user input. You cannot just have audio play without button press or user touch. The issue I have found is not related to this basic understanding.

Thank you,
Sean

Hi,

Does your engine download from github? Is it the latest version of developer branch?

Yes, SimpleAudioEngine.js has been modified a lot recently. Please pull the latest version from github and try again. If the issue still produces, please tell us.

Thanks for your feedback.
David

Hi David,

I have used the latest full builds - 2.2 and 2.2.1 - not the dev branches.

I can run a test with the developer branch if you would like. I will try that and see if I can compile/minify/obfuscate with that version.

I will let you know what I find.

Thank you,
Sean

hi there,

i am using cocos2d version 2.2
i can verify that in file CCconfig.js in directory cocos2d/Platform
in this file, i found this javascript source code line:

cc.ENGINE_VERSION = “Cocos2d-html5-v2.2”;

i am using file SimpleAudioEngine.js

i can see these lines as comments, but i am not sure about the version of this file:

Copyright © 2010-2012 cocos2d-x.org
Copyright © 2008-2010 Ricardo Quesada
Copyright © 2011 Zynga Inc.

my SimpleAudioEngine.js file has the following lines of code:

// beginning of constructor and loadFile() method of my SimpleAudioEngine.js file
var WASound= cc.Class.extend(
{
soundSrc_:null,
ctor:function()
{
this.buffer = null;
},
loadFile : function( url )
{
var request = new XMLHttpRequest();
request.parent = this;
request.open(“GET”, url, true);
request.responseType = “arraybuffer”;
request.parent = this;

        request.onload = function() {
	//request.parent.buffer = WAContext['createBuffer'](1, 22050, 22050);
	request.parent.buffer = WAContext['createBuffer'](2, 22050, 44100);
            request.parent.end = Math.floor(request.parent.buffer.duration);

            request.parent.soundSrc_= WAContext['createBufferSource']();
            request.parent.soundSrc_.buffer = request.parent.buffer;
            request.parent.soundSrc_['connect'](WAContext['destination']);
        }

        request.send();
    },

is this right?
shouldnt i be implementing cors javascript code like in this page? --> http://www.html5rocks.com/en/tutorials/cors/

is responseType fine to be “arraybuffer” for sound files?
can i try another responseType for audio files?
should i be using another version of SimpleAudioEngine.js file?

best regards,

tonci korsano saavedra

hi again,

i should add that i get no errors in safari 5.1.7 or chrome 38 javascript consoles.
there are no javascript errors, but sound is not playing.
i am using another file called AudioSpriteEngine.js with the following play() method:

//beginning of play() method in AudioSpriteEngine.js file
play:function(id, loop, isGameSpecific)
{
if (!g_configManager.soundEnabled)
return;

        if (sys.platform == 'mobile')
        {
            this.listOfPlayedSounds_[id]= cc.AudioEngine.getInstance().playEffect((isGameSpecific ? resPathToSnd : commonResPathToSnd) + id + ".mp3", loop);
            return;
        }

        if (cc.Browser.isMobile && typeof WAContext == "undefined") // keep this check here cc.Browser is undefined under mobile
            return;

        var audioSprite= this[id];

        if (!audioSprite)
            return;

        this.endTime_= audioSprite.start + audioSprite.length;

        try
        {
            if (this.initialized_) {
				audioSpriteEngine.audioObject_= cc.AudioEngine.getInstance()._webAudioMusicObject;
				audioSpriteEngine.audioObject_.loadFile((isGameSpecific ? resPathToSnd : commonResPathToSnd) + id + ".mp3");
				audioSpriteEngine.audioObject_.currentTime= audioSprite.start;
                //this.audioObject_.currentTime= audioSprite.start;
				}
            else
            {
                audioSpriteEngine.updateCallback = function () {
                    audioSpriteEngine.updateCallback = null;
                    audioSpriteEngine.audioObject_.currentTime = audioSprite.start;
                    audioSpriteEngine.audioObject_.play();
                };
            }

            this.audioObject_.play(loop);

            if (typeof WAContext != "undefined") // ugly hack for WA
                this.listOfPlayedSounds_[id]= this.audioObject_.soundSrc_;
            else
                this.listOfPlayedSounds_[id]= 1;
        }
        catch(e)
        {
            audioSpriteEngine.updateCallback = function () {
                audioSpriteEngine.updateCallback = null;
                audioSpriteEngine.audioObject_.currentTime = audioSprite.start;
                audioSpriteEngine.audioObject_.play();
            };
        }
    },

//end of play() method in AudioSpriteEngine.js file

I just checked that there are no javascript errors for the desktop with windows 8.1 I am using.
I also checked it does not in catch section of try/catch block so it is not giving an error at:

            this.audioObject_.play(loop);

play() event logic flow goes to this instruction because I am not using a mobile device, so it skips:

        if (sys.platform == 'mobile')
        {
            this.listOfPlayedSounds_[id]= cc.AudioEngine.getInstance().playEffect((isGameSpecific ? resPathToSnd : commonResPathToSnd) + id + ".mp3", loop);
            return;
        }

and goes to try/catch block in the source code of play() method I am posting.

best regards,

tonci.

I am also facing the same problem, I am able to play sound on all browser except Mobile Safari,
I am using Cocos2dx 3.10 version of js , I am unable to find the SimpleAudioEngine.js file here, I have cc.Audio.js file.

Can you help me with that now?