Audio playback
- Audio loading method: Audio resource
Use AudioSource
- Create an empty node
- In this empty node, add an 'other component' - 'AudioSource'
- Add audioSource to script:
cc.Class({ properties: { audioSource: { type: cc.AudioSource, default: null }, }, play: function () { this.audioSource.play(); }, pause: function () { this.audioSource.pause(); }, });
Use AudioEngine
- Defines a audioClip resource object within the script
Use cc.audioEngine.play (audio, loop, volume);
cc.Class({ properties: { audio: { url: cc.AudioClip, default: null } }, onLoad: function () { this.current = cc.audioEngine.play(this.audio, false, 1); }, onDestroy: function () { cc.audioEngine.stop(this.current); } });
Audio needs a full url, Different from res path. So we recommend avoiding url, try to use audioClip as much as possible to replace url.