(6)、 播放状态的监听
import { _decorator, Component, AudioSource, assert } from 'cc'; const { ccclass, property} = _decorator; @ccclass('AudioSourceControl') export class AudioSourceControl extends Component { @property(AudioSource) public audioSource: AudioSource = null!; onLoad() { // 获取 AudioSource 组件 const audioSource = this.node.getComponent(AudioSource)!; // 检查是否含有 AudioSource,如果没有,则输出错误消息 assert(audioSource); // 将组件赋到全局变量 _audioSource 中 this.audioSource = audioSource; console.log(" this._audioSource==" + this.audioSource) } onEnable () { // Register the started event callback this.audioSource.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this); // Register the ended event callback this.audioSource.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this); } onDisable () { this.audioSource.node.off(AudioSource.EventType.STARTED, this.onAudioStarted, this); this.audioSource.node.off(AudioSource.EventType.ENDED, this.onAudioEnded, this); } onAudioStarted () { console.log("this._audioSource onAudioStarted") } onAudioEnded () { console.log("this._audioSource onAudioEnded") } start() { this.play() } update(deltaTime: number) { } play () { // 播放音乐 this.audioSource.play(); console.log(" this._audioSource play") } pause () { // 暂停音乐 this.audioSource.pause(); } }
4、AudioSource 组件播放音效
音效播放一般有以下特点:
- 播放时间短
- 同时播放的数量多
AudioSource 组件提供 playOneShot
接口来播放音效。
输入参数:
名称 | 类型 | 描述 |
volume |
number | 音量 0-1 |
playOneShot
是一次性播放操作,播放后的音效无法暂停或停止播放,也无法监听播放结束的事件回调。
import { AudioClip, AudioSource, Component, _decorator } from 'cc'; const { ccclass, property } = _decorator; @ccclass("AudioSourceControl") export class AudioSourceControl extends Component { @property(AudioClip) public clip: AudioClip = null!; @property(AudioSource) public audioSource: AudioSource = null!; playOneShot () { this.audioSource.playOneShot(this.clip, 1); } }
playOneShot
是一次性播放操作,播放后的音效无法暂停或停止播放,也无法监听播放结束的事件回调.
5、音频管理器 AudioManager.ts 封装
//AudioManager.ts import { Node, AudioSource, AudioClip, resources, director } from 'cc'; /** * @en * this is a sington class for audio play, can be easily called from anywhere in you project. * @zh * 这是一个用于播放音频的单件类,可以很方便地在项目的任何地方调用。 */ export class AudioManager { private static _inst: AudioManager; public static get inst(): AudioManager { if (this._inst == null) { this._inst = new AudioManager(); } return this._inst; } private _audioSource: AudioSource; constructor() { //@en create a node as AudioManager //@zh 创建一个节点作为 AudioManager let audioMgr = new Node(); audioMgr.name = '__audioMgr__'; //@en add to the scene. //@zh 添加节点到场景 director.getScene().addChild(audioMgr); //@en make it as a persistent node, so it won't be destroied when scene change. //@zh 标记为常驻节点,这样场景切换的时候就不会被销毁了 director.addPersistRootNode(audioMgr); //@en add AudioSource componrnt to play audios. //@zh 添加 AudioSource 组件,用于播放音频。 this._audioSource = audioMgr.addComponent(AudioSource); } public get audioSource() { return this._audioSource; } /** * @en * play short audio, such as strikes,explosions * @zh * 播放短音频,比如 打击音效,爆炸音效等 * @param sound clip or url for the audio * @param volume */ playOneShot(sound: AudioClip | string, volume: number = 1.0) { if (sound instanceof AudioClip) { this._audioSource.playOneShot(sound, volume); } else { resources.load(sound, (err, clip: AudioClip) => { if (err) { console.log(err); } else { this._audioSource.playOneShot(clip, volume); } }); } } /** * @en * play long audio, such as the bg music * @zh * 播放长音频,比如 背景音乐 * @param sound clip or url for the sound * @param volume */ play(sound: AudioClip | string, volume: number = 1.0) { if (sound instanceof AudioClip) { this._audioSource.clip = sound; this._audioSource.play(); this.audioSource.volume = volume; } else { resources.load(sound, (err, clip: AudioClip) => { if (err) { console.log(err); } else { this._audioSource.clip = clip; this._audioSource.play(); this.audioSource.volume = volume; } }); } } /** * stop the audio play */ stop() { this._audioSource.stop(); } /** * pause the audio play */ pause() { this._audioSource.pause(); } /** * resume the audio play */ resume(){ this._audioSource.play(); } }
6、Web 平台的播放限制
目前 Web 平台的音频播放需要遵守最新的 Audio Play Policy,即使 AudioSource 组件设置了 playOnAwake
,也需要在第一次用户点击事件发生后,才会播放:
如图,当音乐开始播放后,有声音图标显示: