cocos2d / cocos2d-html5

Cocos2d for Web Browsers. Built using JavaScript.
https://www.cocos.com
3.07k stars 902 forks source link

cc.audioEngine.playEffect returning null but is playing anyways #3556

Open milomartinez opened 6 years ago

milomartinez commented 6 years ago

Hi , i just found out that if the audio (mp3 in my case) is playing using cc.audioEngine.playEffect when the loading hasn't finished, the sound will play but retuning null as an audio id, the problem is when the sound is playing in a loop, amd there no sound id to stop it. any thouhgt?? cheers

hoanghiep271295 commented 4 years ago

The very easy way to fix this problem, if you check audioId is null or underfined please call cc.audioEngine.stopAllEffects(); -- declare

slot.SoundManager = cc.Class.extend({
    ctor: function () {
    },
    soundEffect: function (url, loop) {
        var audioId = cc.audioEngine.playEffect(url, loop);
        return audioId;
    },
    stopAllEffect: function () {
        cc.audioEngine.stopAllEffects();
    },
    stopEffectByID: function (id) {
        cc.audioEngine.stopEffect(id);
    }
});

--- in use

if (this.audioId) {
            this.soundManager.stopEffectByID(this.audioId);
        } else {
            this.soundManager.stopAllEffect();
        }