AudioController.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. },
  5. play(name)
  6. {
  7. let self = this;
  8. cc.loader.loadRes(name, cc.AudioClip, (err, audioClip)=> {
  9. self.currentAudio = cc.audioEngine.play(audioClip, false, 1);
  10. });
  11. },
  12. playAudio(name,callback)
  13. {
  14. let self = this;
  15. cc.loader.loadRes(name, cc.AudioClip, (err, audioClip)=> {
  16. self.currentAudio = cc.audioEngine.play(audioClip, false, 1);
  17. cc.audioEngine.setFinishCallback(self.currentAudio, function ()
  18. {
  19. callback();
  20. });
  21. });
  22. },
  23. playAudioBySequence(arr,callback)
  24. {
  25. let self = this;
  26. let aIndex = 0;
  27. let playDemonstrationAudio = function () {
  28. if(aIndex == arr.length)
  29. {
  30. callback();
  31. return;
  32. }
  33. if(typeof arr[aIndex] === "number")
  34. {
  35. self.playNumAudio(arr[aIndex],function () {
  36. aIndex++;
  37. playDemonstrationAudio();
  38. });
  39. }
  40. else
  41. {
  42. self.playAudio(arr[aIndex],playDemonstrationAudio);
  43. aIndex++;
  44. }
  45. };
  46. playDemonstrationAudio(arr[aIndex],playDemonstrationAudio);
  47. },
  48. playNumAudio(num,callback)
  49. {
  50. if(num == 15)
  51. {
  52. this.playAudio('Audios/Game/Others/一组15秒',callback);
  53. }
  54. else if(num == 20)
  55. {
  56. this.playAudio('Audios/Game/Others/一组20秒',callback);
  57. }
  58. else if(num == 25)
  59. {
  60. this.playAudio('Audios/Game/Others/一组25秒',callback);
  61. }
  62. else if(num == 30)
  63. {
  64. this.playAudio('Audios/Game/Others/一组30秒',callback);
  65. }
  66. // let numArr = [];
  67. // this.appendNum(num,numArr);
  68. //
  69. // let aIndex = 0;
  70. // let playNum = function () {
  71. // if(aIndex == numArr.length)
  72. // {
  73. // this.unschedule(playNum);
  74. // // console.log('22222')
  75. // this.scheduleOnce(function () {
  76. // callback();
  77. // }, 0.3);
  78. //
  79. // return;
  80. // }
  81. // this.play(numArr[aIndex]);
  82. // aIndex++;
  83. // };
  84. // this.schedule(playNum, 0.3);
  85. },
  86. appendNum(num,arr)
  87. {
  88. if(num == 10)
  89. {
  90. arr.push('Audios/Game/Nums/10');
  91. return;
  92. }
  93. if(num > 10 && num < 20)
  94. {
  95. let str = num.toString();
  96. arr.push('Audios/Game/Nums/10');
  97. arr.push('Audios/Game/Nums/'+str.substr(1,1));
  98. return;
  99. }
  100. let str = num.toString();
  101. arr.push('Audios/Game/Nums/'+str.substr(0,1));
  102. if(str.length>1)
  103. {
  104. arr.push('Audios/Game/Nums/10');
  105. if(str.substr(1,1)!='0')
  106. {
  107. arr.push('Audios/Game/Nums/'+str.substr(1,1));
  108. }
  109. }
  110. }
  111. });