123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import BasicManager from "../Basic/BasicManager";
- import { _IPlaySound } from "./Data/AudioConfig";
- import { _EJsonRes, _IAudioConfig } from "../Data/CommonDataType";
- import EventManager from "../Event/EventManager";
- import { EventName } from "../Event/EventName";
- import JsonResources from "../Resources/JsonResources";
- import TimeManager from "../Time/TimeManager";
- import Tool from "../Tool/Tool";
- /**
- * 音频管理器 - 逻辑层
- */
- export default class AudioManager extends BasicManager {
- /**当前背景音乐 */
- private bgm: string;
- /**音频ID列表 */
- private audioIDs: Map<string, cc.AudioSource>;
- /**当前播放音频ID列表 */
- private currentAudioIDs: Map<string, cc.AudioSource>;
- /**资源表 */
- private res: Array<_IAudioConfig>;
- constructor () {
- super();
- Tool.log("生成音频管理器");
- this.audioIDs = new Map<string, cc.AudioSource>();
- this.currentAudioIDs = new Map<string, cc.AudioSource>();
- //失去舞台焦点(切出游戏)的处理
- EventManager.onEvent(EventName.Phone.Phone_Hide, this, () => {
- cc.audioEngine.pauseAll();
- });
- //获得舞台焦点(切回游戏)的处理
- EventManager.onEvent(EventName.Phone.Phone_Show, this, () => {
- cc.audioEngine.resumeAll();
- });
- EventManager.onEvent(EventName.Audio.Audio_PauseMusic, this, () => {
- cc.audioEngine.pauseMusic();
- });
- EventManager.onEvent(EventName.Audio.Audio_ResumeMusic, this, () => {
- cc.audioEngine.resumeMusic();
- });
- EventManager.onEvent(EventName.Audio.Audio_PauseSound, this, (url: string) => {
- if (url != null) {
- this.audioIDs.get(url) && this.audioIDs.get(url).pause();
- }
- else {
- this.currentAudioIDs.forEach((value: cc.AudioSource, key: string) => {
- value.pause();
- });
- }
- });
- EventManager.onEvent(EventName.Audio.Audio_ResumeSound, this, (url: string) => {
- if (url != null) {
- let value: cc.AudioSource = this.audioIDs.get(url);
- if (value != null) {
- if (value.getCurrentTime() == 0) {
- value.play();
- }
- else {
- value.resume();
- }
- }
- }
- else {
- this.currentAudioIDs.forEach((value: cc.AudioSource, key: string) => {
- if (value.getCurrentTime() == 0) {
- value.play();
- }
- else {
- value.resume();
- }
- });
- }
- });
- EventManager.onEvent(EventName.Audio.Audio_PlayMusic, this, this.onPlayMusic);
- EventManager.onEvent(EventName.Audio.Audio_StopMusic, this, this.onStopMusic);
- EventManager.onEvent(EventName.Audio.Audio_PlaySound, this, this.onPlaySound);
- EventManager.onEvent(EventName.Audio.Audio_StopSound, this, this.onStopSound);
- }
- /**播放背景音乐 */
- public onPlayMusic (url: string): void {
- if (this.res == null) {
- this.res = JsonResources.getResources(_EJsonRes.Audio);
- }
- if (this.bgm == url) {
- if (this.audioIDs.has(url) == true) {
- this.audioIDs.get(url).play();
- }
- return;
- }
- this.bgm = url;
- cc.assetManager.loadBundle("res/audio", (err: Error, prefab: cc.AssetManager.Bundle) => {
- prefab.load(url, cc.AudioClip, (error: Error, clip: cc.AudioClip) => {
- if (this.audioIDs == null || clip == null) {
- return;
- }
- let audioSource: cc.AudioSource;
- if (this.audioIDs.has(url) == true) {
- audioSource = this.audioIDs.get(url);
- }
- else {
- audioSource = new cc.AudioSource();
- audioSource.loop = (this.res[url] != null)? this.res[url].loop:true;
- audioSource.clip = prefab.get(url);
- audioSource.volume = (this.res[url] != null)? this.res[url].volume:1;
- this.audioIDs.set(url, audioSource);
- }
- audioSource.play();
- });
- });
- }
- /**停止背景音乐 */
- private onStopMusic (url: string): void {
- if (this.audioIDs.has(url) == true) {
- this.audioIDs.get(url).stop();
- }
- }
- /**播放音效 */
- public onPlaySound (playSound: _IPlaySound): void {
- if (this.res == null) {
- this.res = JsonResources.getResources(_EJsonRes.Audio);
- }
- if (this.res == null) {
- return;
- }
- cc.assetManager.loadBundle("res/audio", (err: Error, prefab: cc.AssetManager.Bundle) => {
- prefab.load(playSound.url, cc.AudioClip, (error: Error, clip: cc.AudioClip) => {
- if (this.audioIDs == null || clip == null) {
- return;
- }
-
- let audioSource: cc.AudioSource;
- if (this.audioIDs.has(playSound.url) == true) {
- audioSource = this.audioIDs.get(playSound.url);
- }
- else {
- audioSource = cc.director.getScene().getChildByName('Canvas').addComponent(cc.AudioSource);
- audioSource.loop = (this.res[playSound.url] != null)? this.res[playSound.url].loop:false;
- audioSource.clip = prefab.get(playSound.url);
- audioSource.volume = (this.res[playSound.url] != null)? this.res[playSound.url].volume:1;
- this.audioIDs.set(playSound.url, audioSource);
- this.currentAudioIDs.set(playSound.url, audioSource);
- }
- audioSource.play();
- TimeManager.scheduleOnce(this, () => {
- this.currentAudioIDs.delete(playSound.url);
-
- playSound.callback && playSound.callback();
- }, clip.duration);
- });
- });
- }
- /**停止音效 */
- public onStopSound (url: string): void {
- if (this.audioIDs.has(url) == true) {
- this.currentAudioIDs.delete(url);
- this.audioIDs.get(url).stop();
- }
- }
- _destroy () {
- Tool.log("销毁音频管理器");
- cc.audioEngine.uncacheAll();
-
- this.audioIDs = null;
- this.res = null;
- super._destroy();
- }
- }
|