SoundManager.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { _decorator, Component, Node, AudioSourceComponent, loader, AudioClip, IsPowerOf2, url, instantiate, TERRAIN_HEIGHT_BASE } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('SoundManager')
  4. export class SoundManager extends Component {
  5. /**
  6. * 背景音乐通道
  7. */
  8. private musicSoundChannel:AudioChannel;
  9. /**
  10. * 固定通道
  11. */
  12. private soundChannelMap:Map<string,AudioChannel>;
  13. /**
  14. * 重复利用通道
  15. */
  16. private recycleSoundChannelList:AudioChannel[];
  17. private recycleSoundChannelPool:AudioChannel[];
  18. onLoad():void{
  19. SoundManager.instance=this;
  20. this.soundChannelMap=new Map<string,AudioChannel>();
  21. this.recycleSoundChannelList=[];
  22. this.recycleSoundChannelPool=[];
  23. }
  24. start () {
  25. //背景音乐通道
  26. this.musicSoundChannel=new AudioChannel();
  27. this.musicSoundChannel.aSComponent=this.addComponent(AudioSourceComponent);
  28. //最大重复利用通道4个
  29. let channel:AudioChannel;
  30. for (let index = 0; index < 6; index++) {
  31. channel=new AudioChannel();
  32. channel.aSComponent=this.addComponent(AudioSourceComponent);
  33. this.recycleSoundChannelPool.push(channel);
  34. }
  35. }
  36. public PlayMusic(soundPath:string):void{
  37. this.musicSoundChannel.Play(soundPath,true);
  38. }
  39. /**
  40. * 固定通道播放音效
  41. * @param channel 通道
  42. * @param soundPath 音效地址
  43. * @param isLoop 是否循环播放
  44. * @param force 是否在相同音频的情况下强制从头开始播放
  45. */
  46. public PlaySound(channel:string,soundPath:string,isLoop:boolean=false,force:boolean=false):void{
  47. let audioChannel:AudioChannel;
  48. if(this.soundChannelMap.has(channel)==false){
  49. audioChannel=new AudioChannel();
  50. audioChannel.aSComponent=this.addComponent(AudioSourceComponent);
  51. this.soundChannelMap.set(channel,audioChannel);
  52. }else{
  53. audioChannel=this.soundChannelMap.get(channel);
  54. }
  55. audioChannel.Play(soundPath,isLoop,force);
  56. }
  57. /**
  58. * 清理通道
  59. * @param channel
  60. */
  61. public ClearSound(channel:string):void{
  62. if(this.soundChannelMap.has(channel)==false){
  63. return;
  64. }
  65. let audioChannel:AudioChannel=this.soundChannelMap.get(channel);
  66. audioChannel.Dispose();
  67. this.soundChannelMap.delete(channel);
  68. }
  69. /**
  70. * 停止某个固定通道的音效
  71. * @param channel
  72. */
  73. public StopSound(channel:string):void{
  74. if(this.soundChannelMap.has(channel)==false){
  75. return;
  76. }
  77. let audioChannel:AudioChannel=this.soundChannelMap.get(channel);
  78. audioChannel.Stop();
  79. }
  80. /**
  81. * 某个固定频道是否在播放中
  82. * @param channel
  83. */
  84. public ChannelIsPlaying(channel:string):boolean{
  85. if(this.soundChannelMap.has(channel)==false){
  86. return false;
  87. }
  88. let audioChannel:AudioChannel=this.soundChannelMap.get(channel);
  89. return audioChannel.isPlaying;
  90. }
  91. /**
  92. * 通道播放音效
  93. * @param soundPath 音效地址
  94. */
  95. public PlaySoundRecycleChannel(soundPath:string):void{
  96. let audioChannel:AudioChannel;
  97. //检测是否有已经播放完成的通道
  98. for (let index = 0; index < this.recycleSoundChannelList.length; index++) {
  99. const element = this.recycleSoundChannelList[index];
  100. if(element.isPlaying==false){
  101. this.recycleSoundChannelPool.push(element);
  102. this.recycleSoundChannelList.splice(index,1);
  103. }
  104. }
  105. //如果池中没有,则不播放该音效!
  106. if(this.recycleSoundChannelPool.length==0){
  107. return;
  108. }
  109. audioChannel=this.recycleSoundChannelPool.pop();
  110. this.recycleSoundChannelList.push(audioChannel);
  111. //标记为被占有
  112. audioChannel.Play(soundPath,false);
  113. }
  114. // update (deltaTime: number) {
  115. // // Your update function goes here.
  116. // }
  117. private static instance:SoundManager;
  118. public static get single():SoundManager{
  119. return this.instance;
  120. }
  121. }
  122. class AudioChannel{
  123. /**
  124. * 音频资源
  125. */
  126. private static audioClipMap:Map<string,AudioClip>=new Map<string,AudioClip>();
  127. aSComponent:AudioSourceComponent;
  128. private soundUrl:string;
  129. private isLoop:boolean;
  130. private force:boolean;
  131. /**
  132. * 0 空闲 1加载中 2播放中
  133. */
  134. private state:number=0;
  135. constructor(){
  136. }
  137. Play(soundUrl:string,isLoop:boolean,force:boolean=false):void{
  138. if(this.soundUrl==soundUrl&&this.state==1){
  139. return;
  140. }
  141. this.soundUrl=soundUrl;
  142. this.isLoop=isLoop;
  143. this.force=force;
  144. this.state=1;
  145. let key:string=url.raw("resources/"+this.soundUrl);
  146. if(AudioChannel.audioClipMap.has(key)){
  147. let clip:AudioClip=AudioChannel.audioClipMap.get(key);
  148. console.log("播放音频缓存!");
  149. this.__play(clip);
  150. }else{
  151. loader.loadRes(this.soundUrl,AudioClip,this.audioLoadComplete.bind(this));
  152. }
  153. }
  154. private audioLoadComplete(err:Error,asset:AudioClip):void{
  155. if(err){
  156. console.error("加载音乐错误!");
  157. return;
  158. }
  159. let key:string=asset.nativeUrl.replace(asset["_native"],"");
  160. AudioChannel.audioClipMap.set(key,asset);
  161. if(this.state==0){
  162. return;
  163. }
  164. this.__play(asset);
  165. }
  166. private __play(audioClip:AudioClip):void{
  167. this.state=2;
  168. if(this.aSComponent==null){
  169. return;
  170. }
  171. if(this.aSComponent.clip!=audioClip){
  172. this.aSComponent.stop();
  173. this.aSComponent.clip=audioClip;
  174. }
  175. this.aSComponent.loop=this.isLoop;
  176. if(this.force){
  177. this.aSComponent.currentTime=0;
  178. }
  179. this.aSComponent.play();
  180. }
  181. Stop():void{
  182. this.state=0;
  183. this.soundUrl=null;
  184. this.aSComponent.stop();
  185. }
  186. Dispose():void{
  187. this.soundUrl=null;
  188. this.aSComponent.destroy();
  189. this.aSComponent=null;
  190. }
  191. get isPlaying():boolean{
  192. return this.state>0;
  193. }
  194. }