AudioManager.ts 18 KB

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