AudioManager.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. resources.load(sfxPath, AudioClip, (err, clip) => {
  192. if (err) {
  193. console.error(`[AudioManager] 加载${soundType}失败: ${sfxPath}`, err);
  194. return;
  195. }
  196. // 设置临时音量(如果指定)
  197. const originalVolume = audioSource.volume;
  198. if (volume !== undefined) {
  199. audioSource.volume = volume * baseVolume;
  200. }
  201. audioSource.playOneShot(clip);
  202. // 恢复原始音量
  203. if (volume !== undefined) {
  204. audioSource.volume = originalVolume;
  205. }
  206. console.log(`[AudioManager] 播放${soundType}: ${sfxPath}`);
  207. });
  208. }
  209. /**
  210. * 设置音乐音量
  211. * @param volume 音量值(0-1)
  212. */
  213. public setMusicVolume(volume: number) {
  214. this.musicVolume = Math.max(0, Math.min(1, volume));
  215. this.updateMusicVolume();
  216. //console.log(`[AudioManager] 设置音乐音量: ${this.musicVolume}`);
  217. }
  218. /**
  219. * 设置音效音量(向后兼容)
  220. * @param volume 音量值(0-1)
  221. */
  222. public setSoundEffectVolume(volume: number) {
  223. this.soundEffectVolume = Math.max(0, Math.min(1, volume));
  224. this.setUISoundVolume(volume);
  225. //console.log(`[AudioManager] 设置音效音量: ${this.soundEffectVolume}`);
  226. }
  227. /**
  228. * 设置UI音效音量
  229. * @param volume 音量值(0-1)
  230. */
  231. public setUISoundVolume(volume: number) {
  232. this.uiSoundVolume = Math.max(0, Math.min(1, volume));
  233. this.updateUISoundVolume();
  234. }
  235. /**
  236. * 设置敌人音效音量
  237. * @param volume 音量值(0-1)
  238. */
  239. public setEnemySoundVolume(volume: number) {
  240. this.enemySoundVolume = Math.max(0, Math.min(1, volume));
  241. this.updateEnemySoundVolume();
  242. }
  243. /**
  244. * 设置环境音效音量
  245. * @param volume 音量值(0-1)
  246. */
  247. public setEnvironmentSoundVolume(volume: number) {
  248. this.environmentSoundVolume = Math.max(0, Math.min(1, volume));
  249. this.updateEnvironmentSoundVolume();
  250. }
  251. /**
  252. * 设置武器音效音量
  253. * @param volume 音量值(0-1)
  254. */
  255. public setWeaponSoundVolume(volume: number) {
  256. this.weaponSoundVolume = Math.max(0, Math.min(1, volume));
  257. this.updateWeaponSoundVolume();
  258. }
  259. /**
  260. * 设置所有音效音量
  261. * @param volume 音量值(0-1)
  262. */
  263. public setAllSoundVolume(volume: number) {
  264. this.setUISoundVolume(volume);
  265. this.setEnemySoundVolume(volume);
  266. this.setEnvironmentSoundVolume(volume);
  267. this.setWeaponSoundVolume(volume);
  268. this.setSoundEffectVolume(volume);
  269. }
  270. /**
  271. * 获取音乐音量
  272. */
  273. public getMusicVolume(): number {
  274. return this.musicVolume;
  275. }
  276. /**
  277. * 获取音效音量(向后兼容)
  278. */
  279. public getSoundEffectVolume(): number {
  280. return this.soundEffectVolume;
  281. }
  282. /**
  283. * 获取UI音效音量
  284. */
  285. public getUISoundVolume(): number {
  286. return this.uiSoundVolume;
  287. }
  288. /**
  289. * 获取敌人音效音量
  290. */
  291. public getEnemySoundVolume(): number {
  292. return this.enemySoundVolume;
  293. }
  294. /**
  295. * 获取环境音效音量
  296. */
  297. public getEnvironmentSoundVolume(): number {
  298. return this.environmentSoundVolume;
  299. }
  300. /**
  301. * 获取武器音效音量
  302. */
  303. public getWeaponSoundVolume(): number {
  304. return this.weaponSoundVolume;
  305. }
  306. /**
  307. * 更新音乐音量
  308. */
  309. private updateMusicVolume() {
  310. if (this.musicAudioSource) {
  311. this.musicAudioSource.volume = this.musicVolume;
  312. }
  313. }
  314. /**
  315. * 更新UI音效音量
  316. */
  317. private updateUISoundVolume() {
  318. if (this.uiSoundAudioSource) {
  319. this.uiSoundAudioSource.volume = this.uiSoundVolume;
  320. }
  321. }
  322. /**
  323. * 更新敌人音效音量
  324. */
  325. private updateEnemySoundVolume() {
  326. if (this.enemySoundAudioSource) {
  327. this.enemySoundAudioSource.volume = this.enemySoundVolume;
  328. }
  329. }
  330. /**
  331. * 更新环境音效音量
  332. */
  333. private updateEnvironmentSoundVolume() {
  334. if (this.environmentSoundAudioSource) {
  335. this.environmentSoundAudioSource.volume = this.environmentSoundVolume;
  336. }
  337. }
  338. /**
  339. * 更新武器音效音量
  340. */
  341. private updateWeaponSoundVolume() {
  342. if (this.weaponSoundAudioSource) {
  343. this.weaponSoundAudioSource.volume = this.weaponSoundVolume;
  344. }
  345. }
  346. /**
  347. * 更新所有音量
  348. */
  349. private updateAllVolumes() {
  350. this.updateMusicVolume();
  351. this.updateUISoundVolume();
  352. this.updateEnemySoundVolume();
  353. this.updateEnvironmentSoundVolume();
  354. this.updateWeaponSoundVolume();
  355. }
  356. /**
  357. * 静音所有音频
  358. */
  359. public muteAll() {
  360. this.setMusicVolume(0);
  361. this.setAllSoundVolume(0);
  362. }
  363. /**
  364. * 恢复所有音频音量
  365. */
  366. public unmuteAll() {
  367. this.setMusicVolume(0.8);
  368. this.setAllSoundVolume(0.8);
  369. }
  370. /**
  371. * 静音所有音效(保留音乐)
  372. */
  373. public muteAllSounds() {
  374. this.setAllSoundVolume(0);
  375. }
  376. /**
  377. * 恢复所有音效音量(保留音乐)
  378. */
  379. public unmuteAllSounds() {
  380. this.setAllSoundVolume(0.8);
  381. }
  382. /**
  383. * 检查音乐是否正在播放
  384. */
  385. public isMusicPlaying(): boolean {
  386. return this.musicAudioSource && this.musicAudioSource.playing;
  387. }
  388. onDestroy() {
  389. if (AudioManager._instance === this) {
  390. AudioManager._instance = null;
  391. }
  392. }
  393. }
  394. /**
  395. * 音频管理器的静态接口
  396. * 提供便捷的全局访问方法
  397. */
  398. export class Audio {
  399. /**
  400. * 播放背景音乐
  401. */
  402. static playMusic(musicPath: string, loop: boolean = true) {
  403. const manager = AudioManager.getInstance();
  404. if (manager) {
  405. manager.playMusic(musicPath, loop);
  406. }
  407. }
  408. /**
  409. * 播放音效(向后兼容)
  410. */
  411. static playSFX(sfxPath: string, volume?: number) {
  412. const manager = AudioManager.getInstance();
  413. if (manager) {
  414. manager.playSoundEffect(sfxPath, volume);
  415. }
  416. }
  417. /**
  418. * 播放UI音效
  419. */
  420. static playUISound(sfxPath: string, volume?: number) {
  421. const manager = AudioManager.getInstance();
  422. if (manager) {
  423. manager.playUISound(sfxPath, volume);
  424. }
  425. }
  426. /**
  427. * 播放敌人音效
  428. */
  429. static playEnemySound(sfxPath: string, volume?: number) {
  430. const manager = AudioManager.getInstance();
  431. if (manager) {
  432. manager.playEnemySound(sfxPath, volume);
  433. }
  434. }
  435. /**
  436. * 播放环境音效
  437. */
  438. static playEnvironmentSound(sfxPath: string, volume?: number) {
  439. const manager = AudioManager.getInstance();
  440. if (manager) {
  441. manager.playEnvironmentSound(sfxPath, volume);
  442. }
  443. }
  444. /**
  445. * 播放武器音效
  446. */
  447. static playWeaponSound(sfxPath: string, volume?: number) {
  448. const manager = AudioManager.getInstance();
  449. if (manager) {
  450. manager.playWeaponSound(sfxPath, volume);
  451. }
  452. }
  453. /**
  454. * 设置音乐音量
  455. */
  456. static setMusicVolume(volume: number) {
  457. const manager = AudioManager.getInstance();
  458. if (manager) {
  459. manager.setMusicVolume(volume);
  460. }
  461. }
  462. /**
  463. * 设置音效音量(向后兼容)
  464. */
  465. static setSFXVolume(volume: number) {
  466. const manager = AudioManager.getInstance();
  467. if (manager) {
  468. manager.setSoundEffectVolume(volume);
  469. }
  470. }
  471. /**
  472. * 设置UI音效音量
  473. */
  474. static setUISoundVolume(volume: number) {
  475. const manager = AudioManager.getInstance();
  476. if (manager) {
  477. manager.setUISoundVolume(volume);
  478. }
  479. }
  480. /**
  481. * 设置敌人音效音量
  482. */
  483. static setEnemySoundVolume(volume: number) {
  484. const manager = AudioManager.getInstance();
  485. if (manager) {
  486. manager.setEnemySoundVolume(volume);
  487. }
  488. }
  489. /**
  490. * 设置环境音效音量
  491. */
  492. static setEnvironmentSoundVolume(volume: number) {
  493. const manager = AudioManager.getInstance();
  494. if (manager) {
  495. manager.setEnvironmentSoundVolume(volume);
  496. }
  497. }
  498. /**
  499. * 设置武器音效音量
  500. */
  501. static setWeaponSoundVolume(volume: number) {
  502. const manager = AudioManager.getInstance();
  503. if (manager) {
  504. manager.setWeaponSoundVolume(volume);
  505. }
  506. }
  507. /**
  508. * 设置所有音效音量
  509. */
  510. static setAllSoundVolume(volume: number) {
  511. const manager = AudioManager.getInstance();
  512. if (manager) {
  513. manager.setAllSoundVolume(volume);
  514. }
  515. }
  516. /**
  517. * 停止音乐
  518. */
  519. static stopMusic() {
  520. const manager = AudioManager.getInstance();
  521. if (manager) {
  522. manager.stopMusic();
  523. }
  524. }
  525. /**
  526. * 暂停音乐
  527. */
  528. static pauseMusic() {
  529. const manager = AudioManager.getInstance();
  530. if (manager) {
  531. manager.pauseMusic();
  532. }
  533. }
  534. /**
  535. * 恢复音乐
  536. */
  537. static resumeMusic() {
  538. const manager = AudioManager.getInstance();
  539. if (manager) {
  540. manager.resumeMusic();
  541. }
  542. }
  543. /**
  544. * 静音所有音频
  545. */
  546. static muteAll() {
  547. const manager = AudioManager.getInstance();
  548. if (manager) {
  549. manager.muteAll();
  550. }
  551. }
  552. /**
  553. * 恢复所有音频
  554. */
  555. static unmuteAll() {
  556. const manager = AudioManager.getInstance();
  557. if (manager) {
  558. manager.unmuteAll();
  559. }
  560. }
  561. /**
  562. * 静音所有音效(保留音乐)
  563. */
  564. static muteAllSounds() {
  565. const manager = AudioManager.getInstance();
  566. if (manager) {
  567. manager.muteAllSounds();
  568. }
  569. }
  570. /**
  571. * 恢复所有音效(保留音乐)
  572. */
  573. static unmuteAllSounds() {
  574. const manager = AudioManager.getInstance();
  575. if (manager) {
  576. manager.unmuteAllSounds();
  577. }
  578. }
  579. }