audio.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. import { assetManager, AudioClip, AudioSource, director, Node } from "cc";
  2. const ch_log = chsdk.log;
  3. const ch_storage = chsdk.storage;
  4. /**音频等资源加载方式*/
  5. export enum loadType {
  6. /**未知*/
  7. none = 0,
  8. /**bundle*/
  9. bundle = 1,
  10. /**远程*/
  11. remote = 2,
  12. }
  13. /**音频播放控制
  14. * 需要初始化资源加载方式
  15. * loadType.bundle 时需要设置 bundle名
  16. * loadType.remote 测试使用远程的音频需要设置远程地址
  17. */
  18. export default class ch_audio {
  19. private static _instance: ch_audio;
  20. public static getInstance(): ch_audio {
  21. if (!this._instance) this._instance = new ch_audio();
  22. return this._instance;
  23. }
  24. private _volume_music: number = 1;
  25. private _volume_effect: number = 1;
  26. private _switch_music: boolean = true;
  27. private _switch_effect: boolean = true;
  28. private readonly _effect_max: number = 5;
  29. private _effect_index: number = 0;
  30. private _effect_source_pool: AudioSource[] = [];
  31. private _music_source: AudioSource;
  32. private _load_type: loadType = loadType.none;
  33. private _bundle_name: string;
  34. private _remote_url: string;
  35. private _playing_sound: Set<string> = new Set();
  36. constructor() {
  37. const audio = new Node();
  38. audio.name = '__ch_audio__';
  39. director.getScene().addChild(audio);
  40. director.addPersistRootNode(audio);
  41. this._music_source = this._create(audio);
  42. for (let i = 0; i < this._effect_max; i++) {
  43. this._effect_source_pool.push(this._create(audio));
  44. }
  45. this.load();
  46. }
  47. /**
  48. * 创建音频源
  49. * @param node 节点
  50. * @param volume 音量
  51. * @returns AudioSource 音频源组件
  52. */
  53. private _create(node: Node): AudioSource {
  54. const source = node.addComponent(AudioSource);
  55. source.loop = false;
  56. source.playOnAwake = false;
  57. source.volume = 0.5;
  58. return source;
  59. }
  60. /**初始化*/
  61. init(load_type: loadType, bundle_name: string, remote_url: string): void {
  62. this._load_type = load_type;
  63. this._bundle_name = bundle_name;
  64. this._remote_url = remote_url;
  65. }
  66. /**切换bundle*/
  67. set_bundle_name(bundle_name: string): void {
  68. this._bundle_name = bundle_name;
  69. }
  70. /**
  71. * 释放通过 [[load]] 或者 [[loadDir]] 加载的声音资源。
  72. * @param sound 声音资源路径
  73. */
  74. release(sound?: string): void {
  75. if (this._load_type == loadType.none) {
  76. ch_log.warn('音频模块未初始化');
  77. } else if (this._load_type == loadType.bundle) {
  78. const bundle = assetManager.getBundle(this._bundle_name);
  79. if (!sound) {
  80. bundle.releaseAll();
  81. } else {
  82. bundle.release(sound, AudioClip);
  83. }
  84. }
  85. }
  86. /** 保存音乐音效的音量、开关配置数据到本地 */
  87. save() {
  88. const local_data: any = {};
  89. local_data.volume_music = this._volume_music;
  90. local_data.volume_effect = this._volume_effect;
  91. local_data.switch_music = this._switch_music;
  92. local_data.switch_effect = this._switch_effect;
  93. ch_storage.set("ch_audio", local_data);
  94. }
  95. /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
  96. load() {
  97. const local_data = ch_storage.getObject("ch_audio");
  98. if (local_data) {
  99. try {
  100. this.setState(local_data);
  101. }
  102. catch (e) {
  103. this.setStateDefault();
  104. }
  105. }
  106. else {
  107. this.setStateDefault();
  108. }
  109. }
  110. private setState(local_data: { volume_music: number, volume_effect: number, switch_music: boolean, switch_effect: boolean }) {
  111. this.volumeMusic = local_data.volume_music;
  112. this.volumeEffect = local_data.volume_effect;
  113. this.switchMusic = local_data.switch_music;
  114. this.switchEffect = local_data.switch_effect;
  115. }
  116. private setStateDefault() {
  117. this.volumeMusic = 0.8;
  118. this.volumeEffect = 0.8;
  119. this.switchMusic = true;
  120. this.switchEffect = true;
  121. }
  122. /**
  123. * 获取背景音乐音量
  124. */
  125. get volumeMusic(): number {
  126. return this._volume_music;
  127. }
  128. /**
  129. * 设置背景音乐音量
  130. * @param value 音乐音量值
  131. */
  132. set volumeMusic(value: number) {
  133. this._volume_music = value;
  134. this._music_source.volume = value;
  135. }
  136. /**
  137. * 获取背景音乐开关值
  138. */
  139. get switchMusic(): boolean {
  140. return this._switch_music;
  141. }
  142. /**
  143. * 设置背景音乐开关值
  144. * @param value 开关值
  145. */
  146. set switchMusic(value: boolean) {
  147. this._switch_music = value;
  148. if (value == false) this._music_source.stop();
  149. }
  150. /**
  151. * 获取音效音量
  152. */
  153. get volumeEffect(): number {
  154. return this._volume_effect;
  155. }
  156. /**
  157. * 设置获取音效音量
  158. * @param value 音效音量值
  159. */
  160. set volumeEffect(value: number) {
  161. this._volume_effect = value;
  162. for (let i = 0; i < this._effect_source_pool.length; i++) {
  163. this._effect_source_pool[i].volume = this._volume_effect;
  164. }
  165. }
  166. /**
  167. * 获取音效开关值
  168. */
  169. get switchEffect(): boolean {
  170. return this._switch_effect;
  171. }
  172. /**
  173. * 设置音效开关值
  174. * @param value 音效开关值
  175. */
  176. set switchEffect(value: boolean) {
  177. this._switch_effect = value;
  178. if (value == false) {
  179. for (let i = 0; i < this._effect_source_pool.length; i++) {
  180. this._effect_source_pool[i].stop();
  181. }
  182. }
  183. }
  184. /**
  185. * @en
  186. * play short audio, such as strikes,explosions
  187. * @zh
  188. * 播放短音频,比如 打击音效,爆炸音效等
  189. * @param sound clip or url for the audio
  190. * @param interval 同名字音频限制播放间隔(毫秒) (默认:0不限制 特殊系数:>0 <=1 使用音频时间X此系数)
  191. */
  192. playOneShot(sound: AudioClip | string, interval: number = 0, remote_ext: string = '.mp3') {
  193. if (!this._switch_effect) return;
  194. if (sound instanceof AudioClip) {
  195. this.doPlayOneShot(sound, interval);
  196. }
  197. else {
  198. if (this._load_type == loadType.none) {
  199. ch_log.warn('音频模块未初始化');
  200. } else if (this._load_type == loadType.bundle) {
  201. const bundle = assetManager.getBundle(this._bundle_name);
  202. if (!bundle) {
  203. ch_log.warn(`请确保 bundle${this._bundle_name} 已加载`);
  204. } else {
  205. bundle.load(sound, (err, clip: AudioClip) => {
  206. if (err) {
  207. ch_log.error(err);
  208. }
  209. else {
  210. this.doPlayOneShot(clip, interval);
  211. }
  212. });
  213. }
  214. } else if (this._load_type == loadType.remote) {
  215. assetManager.loadRemote(this._remote_url + sound + remote_ext, (err: Error | null, clip: AudioClip) => {
  216. if (err) {
  217. ch_log.error(err);
  218. }
  219. else {
  220. this.doPlayOneShot(clip, interval);
  221. }
  222. });
  223. }
  224. }
  225. }
  226. private doPlayOneShot(clip: AudioClip, interval: number): void {
  227. const name: string = clip.name;
  228. if (interval > 0) {
  229. if (this._playing_sound.has(name)) return;
  230. this._playing_sound.add(name);
  231. const time = interval <= 1 ? clip.getDuration() * interval * 1000 : interval;
  232. setTimeout(() => { this._playing_sound.delete(name); }, time);
  233. this.getNextEffectSource().playOneShot(clip, this._volume_effect);
  234. } else {
  235. this.getNextEffectSource().playOneShot(clip, this._volume_effect);
  236. }
  237. }
  238. private getNextEffectSource(): AudioSource {
  239. const source = this._effect_source_pool[this._effect_index];
  240. this._effect_index = (this._effect_index + 1) % this._effect_max;
  241. return source;
  242. }
  243. /**
  244. * @en
  245. * play long audio, such as the bg music
  246. * @zh
  247. * 播放长音频,比如 背景音乐
  248. * @param sound clip or url for the sound
  249. */
  250. play(sound: AudioClip | string, remote_ext: string = '.mp3') {
  251. if (!this._switch_music) return;
  252. if (sound instanceof AudioClip) {
  253. this._music_source.loop = true;
  254. this._music_source.stop();
  255. this._music_source.clip = sound;
  256. this._music_source.play();
  257. this._music_source.volume = this._volume_music;
  258. }
  259. else {
  260. if (this._load_type == loadType.none) {
  261. ch_log.warn('音频模块未初始化');
  262. } else if (this._load_type == loadType.bundle) {
  263. const bundle = assetManager.getBundle(this._bundle_name);
  264. if (!bundle) {
  265. ch_log.warn(`请确保 bundle${this._bundle_name} 已加载`);
  266. } else {
  267. bundle.load(sound, (err, clip: AudioClip) => {
  268. if (err) {
  269. ch_log.error(err);
  270. }
  271. else {
  272. this._music_source.loop = true;
  273. this._music_source.stop();
  274. this._music_source.clip = clip;
  275. this._music_source.play();
  276. this._music_source.volume = this._volume_music;
  277. }
  278. });
  279. }
  280. } else if (this._load_type == loadType.remote) {
  281. assetManager.loadRemote(this._remote_url + sound + remote_ext, (err: Error | null, clip: AudioClip) => {
  282. if (err) {
  283. ch_log.error(err);
  284. }
  285. else {
  286. this._music_source.loop = true;
  287. this._music_source.stop();
  288. this._music_source.clip = clip;
  289. this._music_source.play();
  290. this._music_source.volume = this._volume_music;
  291. }
  292. });
  293. }
  294. }
  295. }
  296. /**
  297. * stop the audio play
  298. */
  299. stop() {
  300. this._music_source.stop();
  301. for (let i = 0; i < this._effect_source_pool.length; i++) {
  302. this._effect_source_pool[i].stop();
  303. }
  304. }
  305. /**stop and clean */
  306. clean() {
  307. this._music_source.stop();
  308. this._music_source.clip?.name
  309. this._music_source.clip = null;
  310. for (let i = 0; i < this._effect_source_pool.length; i++) {
  311. this._effect_source_pool[i].stop();
  312. this._effect_source_pool[i].clip = null;
  313. }
  314. }
  315. /**
  316. * pause the audio play
  317. */
  318. pause() {
  319. this._music_source.pause();
  320. }
  321. /**
  322. * resume the audio play
  323. */
  324. resume() {
  325. if (!this._switch_music) return;
  326. this._music_source.play();
  327. }
  328. /** 重播当前音乐 */
  329. public replay_music(): void {
  330. this._music_source.stop();
  331. this._music_source.play();
  332. }
  333. }