AudioManager.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. import { _decorator, Component, AudioClip, AudioSource, resources, Node } from 'cc';
  2. import { BundleLoader } from '../Core/BundleLoader';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 音频管理器
  6. * 统一管理游戏中的音乐和音效播放
  7. */
  8. @ccclass('AudioManager')
  9. export class AudioManager extends Component {
  10. // 音频源组件
  11. @property({ type: AudioSource, tooltip: '背景音乐播放器' })
  12. public musicAudioSource: AudioSource = null;
  13. @property({ type: AudioSource, tooltip: 'UI音效播放器' })
  14. public uiSoundAudioSource: AudioSource = null;
  15. @property({ type: AudioSource, tooltip: '敌人音效播放器' })
  16. public enemySoundAudioSource: AudioSource = null;
  17. @property({ type: AudioSource, tooltip: '环境音效播放器' })
  18. public environmentSoundAudioSource: AudioSource = null;
  19. @property({ type: AudioSource, tooltip: '武器音效播放器' })
  20. public weaponSoundAudioSource: AudioSource = null;
  21. // 音量设置
  22. private musicVolume: number = 0.8;
  23. private uiSoundVolume: number = 0.8;
  24. private enemySoundVolume: number = 0.8;
  25. private environmentSoundVolume: number = 0.8;
  26. private weaponSoundVolume: number = 0.8;
  27. // 主音效音量(向后兼容)
  28. private soundEffectVolume: number = 0.8;
  29. // 当前播放的音乐
  30. private currentMusicClip: AudioClip = null;
  31. // Bundle加载器
  32. private bundleLoader: BundleLoader = null;
  33. // 单例实例
  34. private static _instance: AudioManager = null;
  35. onLoad() {
  36. // 设置单例
  37. if (AudioManager._instance === null) {
  38. AudioManager._instance = this;
  39. // 防止场景切换时被销毁
  40. // this.node.parent = null;
  41. // director.addPersistRootNode(this.node);
  42. } else {
  43. this.node.destroy();
  44. return;
  45. }
  46. this.bundleLoader = BundleLoader.getInstance();
  47. this.initializeAudioSources();
  48. }
  49. /**
  50. * 获取单例实例
  51. */
  52. public static getInstance(): AudioManager {
  53. return AudioManager._instance;
  54. }
  55. /**
  56. * 初始化音频源
  57. */
  58. private initializeAudioSources() {
  59. // 如果没有指定音频源,尝试自动创建
  60. if (!this.musicAudioSource) {
  61. const musicNode = new Node('MusicAudioSource');
  62. musicNode.parent = this.node;
  63. this.musicAudioSource = musicNode.addComponent(AudioSource);
  64. this.musicAudioSource.loop = true;
  65. }
  66. if (!this.uiSoundAudioSource) {
  67. const uiSfxNode = new Node('UISoundAudioSource');
  68. uiSfxNode.parent = this.node;
  69. this.uiSoundAudioSource = uiSfxNode.addComponent(AudioSource);
  70. this.uiSoundAudioSource.loop = false;
  71. }
  72. if (!this.enemySoundAudioSource) {
  73. const enemySfxNode = new Node('EnemySoundAudioSource');
  74. enemySfxNode.parent = this.node;
  75. this.enemySoundAudioSource = enemySfxNode.addComponent(AudioSource);
  76. this.enemySoundAudioSource.loop = false;
  77. }
  78. if (!this.environmentSoundAudioSource) {
  79. const envSfxNode = new Node('EnvironmentSoundAudioSource');
  80. envSfxNode.parent = this.node;
  81. this.environmentSoundAudioSource = envSfxNode.addComponent(AudioSource);
  82. this.environmentSoundAudioSource.loop = false;
  83. }
  84. if (!this.weaponSoundAudioSource) {
  85. const weaponSfxNode = new Node('WeaponSoundAudioSource');
  86. weaponSfxNode.parent = this.node;
  87. this.weaponSoundAudioSource = weaponSfxNode.addComponent(AudioSource);
  88. this.weaponSoundAudioSource.loop = false;
  89. }
  90. // 设置初始音量
  91. this.updateAllVolumes();
  92. }
  93. /**
  94. * 播放背景音乐
  95. * @param musicPath 音乐资源路径
  96. * @param loop 是否循环播放
  97. */
  98. public async playMusic(musicPath: string, loop: boolean = true) {
  99. if (!this.musicAudioSource) {
  100. console.warn('[AudioManager] 音乐播放器未初始化');
  101. return;
  102. }
  103. try {
  104. // 转换路径格式:从 "data/弹球音效/fight bgm" 转换为 "弹球音效/fight bgm"
  105. const bundlePath = musicPath.replace('data/', '');
  106. const clip = await this.bundleLoader.loadAssetFromBundle<AudioClip>('data', bundlePath, AudioClip);
  107. this.currentMusicClip = clip;
  108. this.musicAudioSource.clip = clip;
  109. this.musicAudioSource.loop = loop;
  110. this.musicAudioSource.play();
  111. console.log(`[AudioManager] 播放音乐: ${musicPath}`);
  112. } catch (error) {
  113. console.error(`[AudioManager] 加载音乐失败: ${musicPath}`, error);
  114. }
  115. }
  116. /**
  117. * 停止背景音乐
  118. */
  119. public stopMusic() {
  120. if (this.musicAudioSource && this.musicAudioSource.playing) {
  121. this.musicAudioSource.stop();
  122. console.log('[AudioManager] 停止音乐播放');
  123. }
  124. }
  125. /**
  126. * 暂停背景音乐
  127. */
  128. public pauseMusic() {
  129. if (this.musicAudioSource && this.musicAudioSource.playing) {
  130. this.musicAudioSource.pause();
  131. console.log('[AudioManager] 暂停音乐播放');
  132. }
  133. }
  134. /**
  135. * 恢复背景音乐
  136. */
  137. public resumeMusic() {
  138. if (this.musicAudioSource && this.currentMusicClip) {
  139. this.musicAudioSource.play();
  140. console.log('[AudioManager] 恢复音乐播放');
  141. }
  142. }
  143. /**
  144. * 播放音效(向后兼容)
  145. * @param sfxPath 音效资源路径
  146. * @param volume 音量(可选,0-1)
  147. */
  148. public playSoundEffect(sfxPath: string, volume?: number) {
  149. this.playUISound(sfxPath, volume);
  150. }
  151. /**
  152. * 播放UI音效
  153. * @param sfxPath 音效资源路径
  154. * @param volume 音量(可选,0-1)
  155. */
  156. public playUISound(sfxPath: string, volume?: number) {
  157. this.playSound(this.uiSoundAudioSource, sfxPath, volume, this.uiSoundVolume, 'UI音效');
  158. }
  159. /**
  160. * 播放敌人音效
  161. * @param sfxPath 音效资源路径
  162. * @param volume 音量(可选,0-1)
  163. */
  164. public playEnemySound(sfxPath: string, volume?: number) {
  165. this.playSound(this.enemySoundAudioSource, sfxPath, volume, this.enemySoundVolume, '敌人音效');
  166. }
  167. /**
  168. * 播放环境音效
  169. * @param sfxPath 音效资源路径
  170. * @param volume 音量(可选,0-1)
  171. */
  172. public playEnvironmentSound(sfxPath: string, volume?: number) {
  173. this.playSound(this.environmentSoundAudioSource, sfxPath, volume, this.environmentSoundVolume, '环境音效');
  174. }
  175. /**
  176. * 播放武器音效
  177. * @param sfxPath 音效资源路径
  178. * @param volume 音量(可选,0-1)
  179. */
  180. public playWeaponSound(sfxPath: string, volume?: number) {
  181. this.playSound(this.weaponSoundAudioSource, sfxPath, volume, this.weaponSoundVolume, '武器音效');
  182. }
  183. /**
  184. * 通用音效播放方法
  185. * @param audioSource 音频源
  186. * @param sfxPath 音效资源路径
  187. * @param volume 音量(可选,0-1)
  188. * @param baseVolume 基础音量
  189. * @param soundType 音效类型(用于日志)
  190. */
  191. private async playSound(audioSource: AudioSource, sfxPath: string, volume: number | undefined, baseVolume: number, soundType: string) {
  192. if (!audioSource) {
  193. console.warn(`[AudioManager] ${soundType}播放器未初始化`);
  194. return;
  195. }
  196. try {
  197. // 去掉.mp3后缀(如果存在)
  198. const cleanPath = sfxPath.endsWith('.mp3') ? sfxPath.slice(0, -4) : sfxPath;
  199. // 转换路径格式:从 "data/弹球音效/xxx" 转换为 "弹球音效/xxx"
  200. const bundlePath = cleanPath.replace('data/', '');
  201. const clip = await this.bundleLoader.loadAssetFromBundle<AudioClip>('data', bundlePath, AudioClip);
  202. // 设置临时音量(如果指定)
  203. const originalVolume = audioSource.volume;
  204. if (volume !== undefined) {
  205. audioSource.volume = volume * baseVolume;
  206. }
  207. audioSource.playOneShot(clip);
  208. // 恢复原始音量
  209. if (volume !== undefined) {
  210. audioSource.volume = originalVolume;
  211. }
  212. console.log(`[AudioManager] 播放${soundType}: ${cleanPath}`);
  213. } catch (error) {
  214. console.error(`[AudioManager] 加载${soundType}失败: ${sfxPath}`, error);
  215. }
  216. }
  217. /**
  218. * 设置音乐音量
  219. * @param volume 音量值(0-1)
  220. */
  221. public setMusicVolume(volume: number) {
  222. this.musicVolume = Math.max(0, Math.min(1, volume));
  223. this.updateMusicVolume();
  224. //console.log(`[AudioManager] 设置音乐音量: ${this.musicVolume}`);
  225. }
  226. /**
  227. * 设置音效音量(向后兼容)
  228. * @param volume 音量值(0-1)
  229. */
  230. public setSoundEffectVolume(volume: number) {
  231. this.soundEffectVolume = Math.max(0, Math.min(1, volume));
  232. this.setUISoundVolume(volume);
  233. //console.log(`[AudioManager] 设置音效音量: ${this.soundEffectVolume}`);
  234. }
  235. /**
  236. * 设置UI音效音量
  237. * @param volume 音量值(0-1)
  238. */
  239. public setUISoundVolume(volume: number) {
  240. this.uiSoundVolume = Math.max(0, Math.min(1, volume));
  241. this.updateUISoundVolume();
  242. }
  243. /**
  244. * 设置敌人音效音量
  245. * @param volume 音量值(0-1)
  246. */
  247. public setEnemySoundVolume(volume: number) {
  248. this.enemySoundVolume = Math.max(0, Math.min(1, volume));
  249. this.updateEnemySoundVolume();
  250. }
  251. /**
  252. * 设置环境音效音量
  253. * @param volume 音量值(0-1)
  254. */
  255. public setEnvironmentSoundVolume(volume: number) {
  256. this.environmentSoundVolume = Math.max(0, Math.min(1, volume));
  257. this.updateEnvironmentSoundVolume();
  258. }
  259. /**
  260. * 设置武器音效音量
  261. * @param volume 音量值(0-1)
  262. */
  263. public setWeaponSoundVolume(volume: number) {
  264. this.weaponSoundVolume = Math.max(0, Math.min(1, volume));
  265. this.updateWeaponSoundVolume();
  266. }
  267. /**
  268. * 设置所有音效音量
  269. * @param volume 音量值(0-1)
  270. */
  271. public setAllSoundVolume(volume: number) {
  272. this.setUISoundVolume(volume);
  273. this.setEnemySoundVolume(volume);
  274. this.setEnvironmentSoundVolume(volume);
  275. this.setWeaponSoundVolume(volume);
  276. this.setSoundEffectVolume(volume);
  277. }
  278. /**
  279. * 获取音乐音量
  280. */
  281. public getMusicVolume(): number {
  282. return this.musicVolume;
  283. }
  284. /**
  285. * 获取音效音量(向后兼容)
  286. */
  287. public getSoundEffectVolume(): number {
  288. return this.soundEffectVolume;
  289. }
  290. /**
  291. * 获取UI音效音量
  292. */
  293. public getUISoundVolume(): number {
  294. return this.uiSoundVolume;
  295. }
  296. /**
  297. * 获取敌人音效音量
  298. */
  299. public getEnemySoundVolume(): number {
  300. return this.enemySoundVolume;
  301. }
  302. /**
  303. * 获取环境音效音量
  304. */
  305. public getEnvironmentSoundVolume(): number {
  306. return this.environmentSoundVolume;
  307. }
  308. /**
  309. * 获取武器音效音量
  310. */
  311. public getWeaponSoundVolume(): number {
  312. return this.weaponSoundVolume;
  313. }
  314. /**
  315. * 更新音乐音量
  316. */
  317. private updateMusicVolume() {
  318. if (this.musicAudioSource) {
  319. this.musicAudioSource.volume = this.musicVolume;
  320. }
  321. }
  322. /**
  323. * 更新UI音效音量
  324. */
  325. private updateUISoundVolume() {
  326. if (this.uiSoundAudioSource) {
  327. this.uiSoundAudioSource.volume = this.uiSoundVolume;
  328. }
  329. }
  330. /**
  331. * 更新敌人音效音量
  332. */
  333. private updateEnemySoundVolume() {
  334. if (this.enemySoundAudioSource) {
  335. this.enemySoundAudioSource.volume = this.enemySoundVolume;
  336. }
  337. }
  338. /**
  339. * 更新环境音效音量
  340. */
  341. private updateEnvironmentSoundVolume() {
  342. if (this.environmentSoundAudioSource) {
  343. this.environmentSoundAudioSource.volume = this.environmentSoundVolume;
  344. }
  345. }
  346. /**
  347. * 更新武器音效音量
  348. */
  349. private updateWeaponSoundVolume() {
  350. if (this.weaponSoundAudioSource) {
  351. this.weaponSoundAudioSource.volume = this.weaponSoundVolume;
  352. }
  353. }
  354. /**
  355. * 更新所有音量
  356. */
  357. private updateAllVolumes() {
  358. this.updateMusicVolume();
  359. this.updateUISoundVolume();
  360. this.updateEnemySoundVolume();
  361. this.updateEnvironmentSoundVolume();
  362. this.updateWeaponSoundVolume();
  363. }
  364. /**
  365. * 静音所有音频
  366. */
  367. public muteAll() {
  368. this.setMusicVolume(0);
  369. this.setAllSoundVolume(0);
  370. }
  371. /**
  372. * 恢复所有音频音量
  373. */
  374. public unmuteAll() {
  375. this.setMusicVolume(0.8);
  376. this.setAllSoundVolume(0.8);
  377. }
  378. /**
  379. * 静音所有音效(保留音乐)
  380. */
  381. public muteAllSounds() {
  382. this.setAllSoundVolume(0);
  383. }
  384. /**
  385. * 恢复所有音效音量(保留音乐)
  386. */
  387. public unmuteAllSounds() {
  388. this.setAllSoundVolume(0.8);
  389. }
  390. /**
  391. * 检查音乐是否正在播放
  392. */
  393. public isMusicPlaying(): boolean {
  394. return this.musicAudioSource && this.musicAudioSource.playing;
  395. }
  396. onDestroy() {
  397. if (AudioManager._instance === this) {
  398. AudioManager._instance = null;
  399. }
  400. }
  401. }
  402. /**
  403. * 音频管理器的静态接口
  404. * 提供便捷的全局访问方法
  405. */
  406. export class Audio {
  407. /**
  408. * 播放背景音乐
  409. */
  410. static playMusic(musicPath: string, loop: boolean = true) {
  411. const manager = AudioManager.getInstance();
  412. if (manager) {
  413. manager.playMusic(musicPath, loop);
  414. }
  415. }
  416. /**
  417. * 播放音效(向后兼容)
  418. */
  419. static playSFX(sfxPath: string, volume?: number) {
  420. const manager = AudioManager.getInstance();
  421. if (manager) {
  422. manager.playSoundEffect(sfxPath, volume);
  423. }
  424. }
  425. /**
  426. * 播放UI音效
  427. */
  428. static playUISound(sfxPath: string, volume?: number) {
  429. const manager = AudioManager.getInstance();
  430. if (manager) {
  431. manager.playUISound(sfxPath, volume);
  432. }
  433. }
  434. /**
  435. * 播放敌人音效
  436. */
  437. static playEnemySound(sfxPath: string, volume?: number) {
  438. const manager = AudioManager.getInstance();
  439. if (manager) {
  440. manager.playEnemySound(sfxPath, volume);
  441. }
  442. }
  443. /**
  444. * 播放环境音效
  445. */
  446. static playEnvironmentSound(sfxPath: string, volume?: number) {
  447. const manager = AudioManager.getInstance();
  448. if (manager) {
  449. manager.playEnvironmentSound(sfxPath, volume);
  450. }
  451. }
  452. /**
  453. * 播放武器音效
  454. */
  455. static playWeaponSound(sfxPath: string, volume?: number) {
  456. const manager = AudioManager.getInstance();
  457. if (manager) {
  458. manager.playWeaponSound(sfxPath, volume);
  459. }
  460. }
  461. /**
  462. * 设置音乐音量
  463. */
  464. static setMusicVolume(volume: number) {
  465. const manager = AudioManager.getInstance();
  466. if (manager) {
  467. manager.setMusicVolume(volume);
  468. }
  469. }
  470. /**
  471. * 设置音效音量(向后兼容)
  472. */
  473. static setSFXVolume(volume: number) {
  474. const manager = AudioManager.getInstance();
  475. if (manager) {
  476. manager.setSoundEffectVolume(volume);
  477. }
  478. }
  479. /**
  480. * 设置UI音效音量
  481. */
  482. static setUISoundVolume(volume: number) {
  483. const manager = AudioManager.getInstance();
  484. if (manager) {
  485. manager.setUISoundVolume(volume);
  486. }
  487. }
  488. /**
  489. * 设置敌人音效音量
  490. */
  491. static setEnemySoundVolume(volume: number) {
  492. const manager = AudioManager.getInstance();
  493. if (manager) {
  494. manager.setEnemySoundVolume(volume);
  495. }
  496. }
  497. /**
  498. * 设置环境音效音量
  499. */
  500. static setEnvironmentSoundVolume(volume: number) {
  501. const manager = AudioManager.getInstance();
  502. if (manager) {
  503. manager.setEnvironmentSoundVolume(volume);
  504. }
  505. }
  506. /**
  507. * 设置武器音效音量
  508. */
  509. static setWeaponSoundVolume(volume: number) {
  510. const manager = AudioManager.getInstance();
  511. if (manager) {
  512. manager.setWeaponSoundVolume(volume);
  513. }
  514. }
  515. /**
  516. * 设置所有音效音量
  517. */
  518. static setAllSoundVolume(volume: number) {
  519. const manager = AudioManager.getInstance();
  520. if (manager) {
  521. manager.setAllSoundVolume(volume);
  522. }
  523. }
  524. /**
  525. * 停止音乐
  526. */
  527. static stopMusic() {
  528. const manager = AudioManager.getInstance();
  529. if (manager) {
  530. manager.stopMusic();
  531. }
  532. }
  533. /**
  534. * 暂停音乐
  535. */
  536. static pauseMusic() {
  537. const manager = AudioManager.getInstance();
  538. if (manager) {
  539. manager.pauseMusic();
  540. }
  541. }
  542. /**
  543. * 恢复音乐
  544. */
  545. static resumeMusic() {
  546. const manager = AudioManager.getInstance();
  547. if (manager) {
  548. manager.resumeMusic();
  549. }
  550. }
  551. /**
  552. * 静音所有音频
  553. */
  554. static muteAll() {
  555. const manager = AudioManager.getInstance();
  556. if (manager) {
  557. manager.muteAll();
  558. }
  559. }
  560. /**
  561. * 恢复所有音频
  562. */
  563. static unmuteAll() {
  564. const manager = AudioManager.getInstance();
  565. if (manager) {
  566. manager.unmuteAll();
  567. }
  568. }
  569. /**
  570. * 静音所有音效(保留音乐)
  571. */
  572. static muteAllSounds() {
  573. const manager = AudioManager.getInstance();
  574. if (manager) {
  575. manager.muteAllSounds();
  576. }
  577. }
  578. /**
  579. * 恢复所有音效(保留音乐)
  580. */
  581. static unmuteAllSounds() {
  582. const manager = AudioManager.getInstance();
  583. if (manager) {
  584. manager.unmuteAllSounds();
  585. }
  586. }
  587. }