AudioMgr.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. *****Created By Alex 2018 08 24
  3. */
  4. var AudioMgr = cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. bgmVolume:0.5,
  8. sfxVolume:1.0,
  9. bgmAudioID:-1,
  10. musicState:1, //1 表示开启 0 表示关闭
  11. },
  12. // use this for initialization
  13. init: function () {
  14. cc.Mgr.loadSound = false;
  15. var t = cc.sys.localStorage.getItem("bgmVolume");
  16. if(t != null){
  17. this.bgmVolume = parseFloat(t);
  18. }
  19. var t = cc.sys.localStorage.getItem("sfxVolume");
  20. if(t != null){
  21. this.sfxVolume = parseFloat(t);
  22. }
  23. cc.game.on(cc.game.EVENT_HIDE, function () {
  24. console.log("cc.audioEngine.pauseAll");
  25. cc.audioEngine.pauseAll();
  26. });
  27. cc.game.on(cc.game.EVENT_SHOW, function () {
  28. console.log("cc.audioEngine.resumeAll");
  29. cc.audioEngine.resumeAll();
  30. });
  31. },
  32. //获取需要播放某个音效用 根据名字来
  33. // getUrl:function(url){
  34. // return cc.url.raw("resources/sound/" + url + ".mp3");
  35. // },
  36. getUrl: function(url) {
  37. return new Promise((resolve, reject) => {
  38. cc.resources.load("sound/" + url, cc.AudioClip, (err, clip) => {
  39. if (err) {
  40. reject(err);
  41. } else {
  42. resolve(clip);
  43. }
  44. });
  45. });
  46. },
  47. ////播放背景音乐
  48. // playBGM:function(url){
  49. // var audioUrl = this.getUrl(url);
  50. // if(this.bgmAudioID >= 0){
  51. // cc.audioEngine.stop(this.bgmAudioID);
  52. // }
  53. // this.bgmAudioID = cc.audioEngine.play(audioUrl,true,this.bgmVolume);
  54. // },
  55. // //播放音效
  56. // playSFX:function(url){
  57. // var audioUrl = this.getUrl(url);
  58. // cc.audioEngine.play(audioUrl,false,this.sfxVolume);
  59. // },
  60. // 播放背景音乐
  61. playBGM: function(url) {
  62. this.getUrl(url).then((clip) => {
  63. if (this.bgmAudioID >= 0) {
  64. cc.audioEngine.stop(this.bgmAudioID);
  65. }
  66. this.bgmAudioID = cc.audioEngine.play(clip, true, this.bgmVolume);
  67. }).catch((err) => {
  68. console.error("zh:Failed to load BGM clip:", err);
  69. });
  70. },
  71. // 播放音效
  72. playSFX: function(url) {
  73. this.getUrl(url).then((clip) => {
  74. cc.audioEngine.play(clip, false, this.sfxVolume);
  75. }).catch((err) => {
  76. console.error("zh:Failed to load SFX clip:", err);
  77. });
  78. },
  79. //设置音效大小
  80. setSFXVolume:function(v){
  81. if(this.sfxVolume != v){
  82. cc.sys.localStorage.setItem("sfxVolume",v);
  83. this.sfxVolume = v;
  84. }
  85. },
  86. //设置背景音大小
  87. setBGMVolume:function(v,force){
  88. if(this.bgmAudioID >= 0){
  89. if(v > 0){
  90. cc.audioEngine.resume(this.bgmAudioID);
  91. }
  92. else{
  93. cc.audioEngine.pause(this.bgmAudioID);
  94. }
  95. }
  96. if(this.bgmVolume != v || force){
  97. cc.sys.localStorage.setItem("bgmVolume",v);
  98. this.bgmVolume = v;
  99. cc.audioEngine.setVolume(this.bgmAudioID,v);
  100. }
  101. },
  102. //暂停
  103. pauseAll:function(){
  104. this.musicState = 0;
  105. this.bgmVolume = 0.0;
  106. this.sfxVolume = 0.0;
  107. cc.audioEngine.pauseAll();
  108. },
  109. //恢复
  110. resumeAll:function(){
  111. this.musicState = 1;
  112. this.bgmVolume = 0.5;
  113. this.sfxVolume = 1.0;
  114. cc.audioEngine.resumeAll();
  115. },
  116. getVoiceState:function(){
  117. return this.musicState;
  118. },
  119. });