Audio playback
- Audio loading method: Audio resource
Use AudioSource component
- 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: {
default: null,
type: cc.AudioClip
}
},
onLoad: function () {
this.current = cc.audioEngine.play(this.audio, false, 1);
},
onDestroy: function () {
cc.audioEngine.stop(this.current);
}
});
Audio needs an AudioClip object, not url. So we recommend avoiding url, try to use audioClip as much as possible to replace url.