SoundManager.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * 声音管理类
  3. * @author xiongjian
  4. * @date 2016/6/30
  5. */
  6. import { SingleClass } from "./SingleClass";
  7. import { dogType } from "./GameInfo";
  8. export class SoundManager extends SingleClass {
  9. private soundList = {}; //声音列表
  10. private soundActList = {}; //动作列表
  11. private actChannelList = {}; //动作音道列表
  12. private EffectAudioID: number;//音效的ID
  13. private bgmChannelID: number; //背景音声道
  14. private clockChannelID: number; //闹铃音声道
  15. private actchannelID: number; //动作音道
  16. private actSound: cc.AudioClip;//动作声音
  17. private _allowPlayEffect: boolean = true; //是否允许播放音效
  18. private _allowPlayBGM: boolean = true; //是否允许播放背景音乐
  19. private _effectVolume: number = 1; //音效音量
  20. private _bgmVolume: number = 1; //背景音量
  21. private jumpID: number = -1; //跳跃音道
  22. //大厅背景音乐, //菜单背景音乐, //游戏背景音乐
  23. public static hallBgm: string[] = ["bgm_1.mp3", "bgm_2.mp3", "bgm_3.mp3"] ;
  24. public static click: string = "click.mp3";//按钮点击
  25. public static start: string = "start.mp3";// 开始游戏
  26. public static addCoin: string = "addCoin.mp3";//
  27. public static cookingOver: string = "cookingOver.mp3";//
  28. public static success: string[] = ["success_1.mp3", "success_2.mp3"]; // 胜利音效
  29. public static fail: string = "fail_1.mp3";// 失败音效
  30. public constructor() {
  31. super();
  32. }
  33. /**将牌值转换未音效名 */
  34. private changeCardValue(cardValue) {
  35. return
  36. }
  37. /**
  38. * 播放音效
  39. * @param soundName 声音名
  40. * @param loops 循环次数
  41. */
  42. public playEffect(soundName: string, loops: boolean = false) {
  43. if (!this.allowPlayEffect) {
  44. return;
  45. }
  46. //从声音列表中获取,声音列表中不存在,则从加载资源中获取
  47. var sound: cc.AudioClip = this.soundList[soundName];
  48. if (sound == null) {
  49. cc.loader.loadRes("Audio/" + soundName, cc.AudioClip, (err, audioClip) => {
  50. console.log("==>" + typeof audioClip);
  51. this.soundList[soundName] = audioClip;
  52. this.EffectAudioID = cc.audioEngine.playEffect(audioClip, loops);
  53. cc.audioEngine.setVolume(this.EffectAudioID, this._effectVolume);
  54. });
  55. }
  56. else {
  57. this.EffectAudioID = cc.audioEngine.playEffect(sound, loops);
  58. cc.audioEngine.setVolume(this.EffectAudioID, this._effectVolume);
  59. }
  60. }
  61. private videoEffect: boolean = false;
  62. private videoMusic: boolean = false;
  63. /**观看视频音量关闭 */
  64. public VideoStartStop() {
  65. this.videoEffect = this._allowPlayEffect;
  66. this.videoMusic = this._allowPlayBGM;
  67. this._allowPlayEffect = false;
  68. this.stopBGM();
  69. }
  70. /**观看视频音量开启*/
  71. public VideoEndOpen() {
  72. if (this.videoMusic) this.playBGM(SoundManager.hallBgm[2]);
  73. this._allowPlayEffect = this.videoEffect;
  74. }
  75. /**
  76. * 停止播放音效
  77. */
  78. public stopEffect() {
  79. cc.audioEngine.stopAllEffects();
  80. // if (!this.EffectAudioID) return;
  81. // cc.audioEngine.stopEffect(this.EffectAudioID)
  82. }
  83. /**
  84. * 播放背景音乐
  85. * @param bgmName 背景音名
  86. * @param startTime 播放起始位置
  87. * @param loops 循环次数
  88. */
  89. public playBGM(bgmName: string, loops: boolean = true) {
  90. if (this.allowPlayBGM == false) {// || this.bgmChannelID != null
  91. return;
  92. }
  93. this.stopBGM();
  94. console.log('播放背景音乐:bgmName', bgmName)
  95. var bgm: cc.AudioClip = this.soundList[bgmName];
  96. if (bgm == null) {
  97. cc.loader.loadRes("Audio/" + bgmName, cc.AudioClip, (err, audioClip) => {
  98. console.log("==>" + typeof audioClip);
  99. this.soundList[bgmName] = audioClip;
  100. // this.bgmChannelID = cc.audioEngine.play(audioClip, loops,this._bgmVolume);
  101. this.bgmChannelID = cc.audioEngine.playMusic(audioClip, loops);
  102. cc.audioEngine.setVolume(this.bgmChannelID, this._bgmVolume);
  103. });
  104. }
  105. if (bgm) {
  106. this.bgmChannelID = cc.audioEngine.playMusic(bgm, loops);
  107. cc.audioEngine.setVolume(this.bgmChannelID, this._bgmVolume);
  108. }
  109. }
  110. /**停止背景音乐*/
  111. public stopBGM() {
  112. // if (this.bgmChannelID) {
  113. cc.audioEngine.stopMusic();
  114. // cc.audioEngine.stop(this.bgmChannelID);
  115. this.bgmChannelID = null;
  116. // }
  117. }
  118. /**停止背景音乐*/
  119. public stopClock() {
  120. if (this.clockChannelID) {
  121. cc.audioEngine.stop(this.clockChannelID);
  122. this.clockChannelID = null;
  123. }
  124. }
  125. /**获取是否允许播放音效*/
  126. public get allowPlayEffect() {
  127. return this._allowPlayEffect;
  128. }
  129. /**设置是否允许播放音效*/
  130. public set allowPlayEffect(bAllow: boolean) {
  131. this._allowPlayEffect = bAllow;
  132. }
  133. /**获取是否允许播放背景音*/
  134. public get allowPlayBGM() {
  135. return this._allowPlayBGM;
  136. }
  137. /**设置是否允许播放背景音*/
  138. public set allowPlayBGM(bAllow: boolean) {
  139. this._allowPlayBGM = bAllow;
  140. if (this._allowPlayBGM == false) {
  141. this.stopBGM();
  142. } else {
  143. this.playBGM(SoundManager.hallBgm[0]);
  144. }
  145. }
  146. /**获取音效音量*/
  147. public get effectVolume() {
  148. return this._effectVolume;
  149. }
  150. /**设置音效音量*/
  151. public set effectVolume(value: number) {
  152. this._effectVolume = value;
  153. }
  154. /**获取BGM音量*/
  155. public get bgmVolume() {
  156. return this._bgmVolume;
  157. }
  158. /**设置BGM音量*/
  159. public set bgmVolume(value: number) {
  160. this._bgmVolume = value;
  161. if (this.bgmChannelID) {
  162. cc.audioEngine.setVolume(this.bgmChannelID, this._bgmVolume);
  163. }
  164. }
  165. }