代码都有注释不再赘述

import { _decorator, Node, AudioClip, AudioSource, game, find } from "cc";
import { Dictionary } from "../core/Dictionary";const { ccclass, property } = _decorator;/**最多有几个音效播放器*/
const MAX_SOUNDS: number = 8/*** 音效管理器*/
@ccclass("AudioMgr")
export class AudioMgr {static _instance: AudioMgr;static get ins() {if (this._instance) {return this._instance;}this._instance = new AudioMgr();return this._instance;}/**音效的常驻节点*/private _persistRootNode: Node = null;//bgm音效全局唯一一个private _music: AudioSource = null//sound音效可以有多个private _sounds: AudioSource[] = null/**bgm静音 0没静音 1静音*/private _music_muted: number = 0/**sound静音 0没静音 1静音*/private _sound_muted: number = 0/**bgm音量*/private _music_volume: number = 1/**sound音量*/private _sound_volume: number = 1/**当前播放的音效索引用以控制使用不同的AudioSource*/private _now_soundid: number = 0/**当前播放的bgm音效名字*/private _cur_music_name: string = ""//存储所有的音效片段private music_clips: Dictionary<string, AudioClip>init() {if (this._persistRootNode) return; //避免切换场景初始化报错this._persistRootNode = find("Canvas")game.addPersistRootNode(this._persistRootNode)this._sounds = []this.music_clips = new Dictionary<string, AudioClip>()/*** 读取本地存储的数据* */this._music = this._persistRootNode.addComponent(AudioSource)//获取bgm的音量this._music.volume = this._music_volume//获取bgm是否存储静音this._music.volume = this._music_muted == 1 ? 0 : this._music_volume//获取sounds列表for (let i = 0; i < MAX_SOUNDS; i++) {this._sounds[i] = this._persistRootNode.addComponent(AudioSource)this._sounds[i].volume = this._sound_volumethis._sounds[i].volume = this._sound_muted == 1 ? 0 : this._sound_volume}}/*** @param audioName 音效名字* @param isLoop 是否循环播放* @protected 播放音效*/playMusic(audioName: string, isLoop: boolean = true) {if (this._cur_music_name == audioName) {return}let call = (clip) => {this._music.clip = nullthis._music.clip = clipthis._music.loop = isLoopthis._music.play()if (!this.music_clips.containsKey(audioName)) {this.music_clips.add(audioName, clip)}}if (this.music_clips.containsKey(audioName)) {call(this.music_clips.getValue(audioName))} else {let bundleName = "base.audio"let path = bundleName + "/" + audioNameassetManager.loadBundle(bundleName,(err, bundle) => {bundle.load(path,AudioClip,(err: any, clip: any)=>{if (err) {console.error("loadAudioClip" + err);}else{call(clip)}});})}}/*** @param audioName 音效名字* @param isLoop 是否循环播放* @protected 播放音效*/playSound(audioName: string, isLoop: boolean = false) {let call = (clip) => {this._sounds[this._now_soundid].clip = nullthis._sounds[this._now_soundid].clip = clipthis._sounds[this._now_soundid].loop = isLoopthis._sounds[this._now_soundid].play()if (!this.music_clips.containsKey(audioName)) {this.music_clips.add(audioName, clip)}this._now_soundid = this._now_soundid + 1 >= this._sounds.length ? 0 : this._now_soundid + 1}if (this.music_clips.containsKey(audioName)) {call(this.music_clips.getValue(audioName))} else {let bundleName = "base.audio"let path = bundleName + "/" + audioNameassetManager.loadBundle(bundleName,(err, bundle) => {bundle.load(path,AudioClip,(err: any, clip: any)=>{if (err) {console.error("loadAudioClip" + err);}else{call(clip)}});})}}/*** 停止播放bgm*/stopMusic() {this._music.stop()this._music.clip = null}/*** 停止播放所有的sound*/stopAllSound() {for (let i = 0; i < this._sounds.length; i++) {this._sounds[i].stop()this._sounds[i].clip = null}this._now_soundid = 0}/*** * @param mute 是否静音music*/setMusicMute(mute: boolean) {if (mute == (this._music_muted == 1)) {return}this._music_muted = mute ? 1 : 0this._music.volume = mute ? this._music_volume : 0//存储music静音 this._music_muted}/*** * @param mute 是否静音sound*/setSoundMute(mute: boolean) {if (mute == (this._sound_muted == 1)) {return}this._sound_muted = mute ? 1 : 0for (let i = 0; i < this._sounds.length; i++) {this._sounds[i].volume = mute ? this._sound_volume : 0}//存储sound静音 this._sound_muted}/*** * @param value 设置音乐声音大小*/setMusicVolume(value: number) {this._music.volume = valuethis._music_volume = value//存储music音量大小 this._music_volume}/*** * @param value 设置sound声音大小*/setSoundVolume(value: number) {this._sound_volume = valuefor (let i = 0; i < this._sounds.length; i++) {this._sounds[i].volume = value}//存储sound音量大小 this._sound_volume}/*** * @returns 返回bgm静音状态*/getMusicMute() {return this._music_muted}/*** * @returns 返回sound音效静音状态*/getSoundMute() {return this._sound_muted}/*** * @returns 返回bgm声音大小*/getMusicVolume() {return this._music_volume}/*** * @returns 返回sound音效声音大小*/getSoundVolume() {return this._sound_volume}
}

Dictionary请查看另外一篇文章CocosCreator Dictionary_小张不爱写代码的博客-CSDN博客

CocosCreator 音效管理器相关推荐

  1. unity音效管理器

    unity游戏制作时,经常需要播放音效,如果没有一个音效管理类,随时随地都可以播放音乐,不利于代码的管理.我们可以将所有关于音效集中到一个管理类当中,可以将这个音效管理器写成单例,也可以将它当作一个模 ...

  2. OneAlive--游戏音效管理器

    /* 游戏音效管理器* 功能:管理游戏中音效的播放和声音的调节* audioSourceBG是背景(2D)音乐,挂在相机上即可* gamesound是游戏音效(3D)挂在人物角色身上*/using S ...

  3. 【Unity植物大战僵尸】音效管理器开发(二十四)

    42.音效管理器开发 导入资源 整理一下GameManager.cs,因为这个脚本要在两个场景中都共用,所以需要把CurrLV相关的移植到LVManager.cs中去,这里就不显示了,说下如何共用,首 ...

  4. Unity 音效管理器编辑窗口的创建和Manager的创建

    一.音效管理器面板的创建 1.首先要继承EditorWindow,然后再静态方法里创建窗口编辑器. [MenuItem("Manager/AudioManager")] stati ...

  5. U3D客户端框架之 音效管理器 与 Fmod介绍安装导入Unity

    一.Fmod介绍与安装导入Unity 1.Fmod与Unity内置Audio播放器对比 Unity内置的Audio底层使用的是FMOD,但是功能不够齐全,高级一点的功能如混合(Mix)等无法使用: 音 ...

  6. Unity 项目中的音效管理器

    using UnityEngine; using System.Collections; using System.Collections.Generic;/// <summary> // ...

  7. CocosCreator 音效管理

    1 创建音效管理类 SoundMgr.ts const {ccclass, property} = cc._decorator;@ccclass export default class SoundM ...

  8. unity音效管理器实现(二)之美

    using System.Collections; using System.Collections.Generic; using UnityEngine; /* 功能:用来对项目中的所有音频做同一的 ...

  9. Unity编程笔记----音效管理器

    游戏世界里离不开各种酷炫的音效,把这些音效集中起来进行管理是每个项目必须要做的事情. 分享一下自用的SoundMgr(适用于大多Unity项目) 整体思路: SoundMgr脚本被调用时,自动执行继承 ...

最新文章

  1. 在main()之前,IAR都做了啥?
  2. CG CTF WEB 密码重置
  3. 利用insert、update和delete命令可以同时对多个表进行操作_使用自然语言进行程序合成...
  4. Excel中数据透视表的 使用 创建(ピポットテーブル)
  5. 单片机定时器之改良版:时间轮定时器
  6. 计算机四级等级考试网络工程师知识点-【操作系统原理+计算机网络两科完整】
  7. 【3D动态脑图制作软件】万彩脑图大师教程 | 将思维导图输出到云服务
  8. 笔记本右侧键盘数字打不出来
  9. 我的世界服务器怎么无限刷红石,我的世界无限红石怎么做 无限红石BUG攻略
  10. x264参数与代码对应
  11. 在七牛云建对象存储用于上传图片
  12. 数据结构——二叉树遍历和常见问题
  13. ERIC6 打开项目报错
  14. 尚硅谷Vue3(天禹老师主讲)的笔记
  15. 【开源WebGIS】07-Openlayers+Vue 测量功能-02
  16. 我的物联网项目(二十九) 线上前期运营
  17. 英国电信将保留EE品牌
  18. Linux与Python经验总结
  19. 安卓来电归属地_原生 Android 也能远离骚扰电话:开源应用来电信息
  20. xjoi1192统计字符

热门文章

  1. Atitti 住房部建设指南
  2. 网页期末作业制作一个简单HTML电影网页设计(HTML+CSS)
  3. ChatGPT中文网
  4. Google Firebase ANE 使用教程
  5. 调用百度h5活体检测,参数video_base64,视频解析错误的问题
  6. 利用FFmpeg玩转Android视频录制与压缩
  7. 中国500强排行榜数据爬取,看看都有哪些大佬
  8. autojs编写的网易云音乐和视频搜索脚本源代码免费分享开源
  9. Ubuntu 17.10 中文无忧版
  10. 三万五千字长文!让你懂透编译原理(六)——第六章 属性文法和语法制导翻译