Audio Playback Examples

Since Cocos Creator 3.x removes the v2.x cc.audioEngine series API and uses AudioSource to control audio playback, it is necessary to declare the AudioSource component as the resident root node and wrap a manager to use it. You can refer to the following code:

  1. import { assert, AudioSource, Component, game } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('GameRoot')
  4. export class GameRoot extends Component {
  5. @property(AudioSource)
  6. _audioSource: AudioSource = null!
  7. onLoad () {
  8. const audioSource = this.node.getComponent(AudioSource)! ;
  9. assert(audioSource);
  10. this._audioSource = audioSource;
  11. // Declare the resident root node, which will not be destroyed in a scene switch. The target node must be the root node, otherwise it is invalid.
  12. game.addPersistRootNode(this.node);
  13. // Wrap the node in the manager.
  14. audioManager.instance.init(this._audioSource);
  15. }
  16. }

The specific implementation of the audio manager can be found in the following code:

  1. import { AudioClip, AudioSource, assert, warn, clamp01, resources } from "cc";
  2. export class audioManager {
  3. private static _instance: audioManager;
  4. private static _audioSource?: AudioSource;
  5. static get instance () {
  6. if (this._instance) {
  7. return this._instance;
  8. }
  9. this._instance = new audioManager();
  10. return this._instance;
  11. }
  12. /**Manager initialization*/
  13. init (audioSource: AudioSource) {
  14. audioManager._audioSource = audioSource;
  15. }
  16. /**
  17. * Play music
  18. * @param {Boolean} loop Whether to loop
  19. */
  20. playMusic (loop: boolean) {
  21. const audioSource = audioManager._audioSource!
  22. assert(audioSource, 'AudioManager not inited!');
  23. audioSource.loop = loop;
  24. if (!audioSource.playing) {
  25. audioSource.playing();
  26. }
  27. }
  28. /**
  29. * Play a sound effect
  30. * @param {String} name The name of the sound effect
  31. * @param {Number} volumeScale Playback volume multiplier
  32. */
  33. playSound (name: string, volumeScale: number = 1 ) {
  34. const audioSource = audioManager._audioSource!
  35. assert(audioSource, 'AudioManager not inited!');
  36. // Note that the second parameter "volumeScale" is a multiple of the playback volume, the final playback volume is "audioSource.volume * volumeScale"
  37. audioSource.playOneShot(audioClip, volumeScale);
  38. }
  39. // Set the music volume
  40. setMusicVolume (flag: number) {
  41. const audioSource = audioManager._audioSource!
  42. assert(audioSource, 'AudioManager not inited!');
  43. flag = clamp01(flag);
  44. audioSource.volume = flag;
  45. }
  46. }

The above code snippet is just an example of one way to use the AudioSource component for playback, and is not complete. Cocos Creator provides a complete example of how to use the wrapped audio playback manager in the tutorial-taxi-game project. Developers can open the Project page in Dashboard, click the New button in the bottom right corner to enter the New Project page, and find the Example Taxi Game, fill in the project name and project location as needed to create and open.

audioEdit