audio.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. --[[
  2. Copyright (c) 2011-2014 chukong-inc.com
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ]]
  19. local audio = {}
  20. local engine = cc.SimpleAudioEngine:getInstance()
  21. function audio.getMusicVolume()
  22. local volume = engine:getMusicVolume()
  23. if DEBUG > 1 then
  24. printf("[audio] getMusicVolume() - volume: %0.2f", volume)
  25. end
  26. return volume
  27. end
  28. function audio.setMusicVolume(volume)
  29. volume = checknumber(volume)
  30. if DEBUG > 1 then
  31. printf("[audio] setMusicVolume() - volume: %0.2f", volume)
  32. end
  33. engine:setMusicVolume(volume)
  34. end
  35. function audio.preloadMusic(filename)
  36. assert(filename, "audio.preloadMusic() - invalid filename")
  37. if DEBUG > 1 then
  38. printf("[audio] preloadMusic() - filename: %s", tostring(filename))
  39. end
  40. engine:preloadMusic(filename)
  41. end
  42. function audio.playMusic(filename, isLoop)
  43. assert(filename, "audio.playMusic() - invalid filename")
  44. if type(isLoop) ~= "boolean" then isLoop = true end
  45. audio.stopMusic()
  46. if DEBUG > 1 then
  47. printf("[audio] playMusic() - filename: %s, isLoop: %s", tostring(filename), tostring(isLoop))
  48. end
  49. engine:playMusic(filename, isLoop)
  50. end
  51. function audio.stopMusic(isReleaseData)
  52. isReleaseData = checkbool(isReleaseData)
  53. if DEBUG > 1 then
  54. printf("[audio] stopMusic() - isReleaseData: %s", tostring(isReleaseData))
  55. end
  56. engine:stopMusic(isReleaseData)
  57. end
  58. function audio.pauseMusic()
  59. if DEBUG > 1 then
  60. printf("[audio] pauseMusic()")
  61. end
  62. engine:pauseMusic()
  63. end
  64. function audio.resumeMusic()
  65. if DEBUG > 1 then
  66. printf("[audio] resumeMusic()")
  67. end
  68. engine:resumeMusic()
  69. end
  70. function audio.rewindMusic()
  71. if DEBUG > 1 then
  72. printf("[audio] rewindMusic()")
  73. end
  74. engine:rewindMusic()
  75. end
  76. function audio.isMusicPlaying()
  77. local ret = engine:isMusicPlaying()
  78. if DEBUG > 1 then
  79. printf("[audio] isMusicPlaying() - ret: %s", tostring(ret))
  80. end
  81. return ret
  82. end
  83. function audio.getSoundsVolume()
  84. local volume = engine:getEffectsVolume()
  85. if DEBUG > 1 then
  86. printf("[audio] getSoundsVolume() - volume: %0.1f", volume)
  87. end
  88. return volume
  89. end
  90. function audio.setSoundsVolume(volume)
  91. volume = checknumber(volume)
  92. if DEBUG > 1 then
  93. printf("[audio] setSoundsVolume() - volume: %0.1f", volume)
  94. end
  95. engine:setEffectsVolume(volume)
  96. end
  97. function audio.playSound(filename, isLoop)
  98. if not filename then
  99. printError("audio.playSound() - invalid filename")
  100. return
  101. end
  102. if type(isLoop) ~= "boolean" then isLoop = false end
  103. if DEBUG > 1 then
  104. printf("[audio] playSound() - filename: %s, isLoop: %s", tostring(filename), tostring(isLoop))
  105. end
  106. return engine:playEffect(filename, isLoop)
  107. end
  108. function audio.pauseSound(handle)
  109. if not handle then
  110. printError("audio.pauseSound() - invalid handle")
  111. return
  112. end
  113. if DEBUG > 1 then
  114. printf("[audio] pauseSound() - handle: %s", tostring(handle))
  115. end
  116. engine:pauseEffect(handle)
  117. end
  118. function audio.pauseAllSounds()
  119. if DEBUG > 1 then
  120. printf("[audio] pauseAllSounds()")
  121. end
  122. engine:pauseAllEffects()
  123. end
  124. function audio.resumeSound(handle)
  125. if not handle then
  126. printError("audio.resumeSound() - invalid handle")
  127. return
  128. end
  129. if DEBUG > 1 then
  130. printf("[audio] resumeSound() - handle: %s", tostring(handle))
  131. end
  132. engine:resumeEffect(handle)
  133. end
  134. function audio.resumeAllSounds()
  135. if DEBUG > 1 then
  136. printf("[audio] resumeAllSounds()")
  137. end
  138. engine:resumeAllEffects()
  139. end
  140. function audio.stopSound(handle)
  141. if not handle then
  142. printError("audio.stopSound() - invalid handle")
  143. return
  144. end
  145. if DEBUG > 1 then
  146. printf("[audio] stopSound() - handle: %s", tostring(handle))
  147. end
  148. engine:stopEffect(handle)
  149. end
  150. function audio.stopAllSounds()
  151. if DEBUG > 1 then
  152. printf("[audio] stopAllSounds()")
  153. end
  154. engine:stopAllEffects()
  155. end
  156. audio.stopAllEffects = audio.stopAllSounds
  157. function audio.preloadSound(filename)
  158. if not filename then
  159. printError("audio.preloadSound() - invalid filename")
  160. return
  161. end
  162. if DEBUG > 1 then
  163. printf("[audio] preloadSound() - filename: %s", tostring(filename))
  164. end
  165. engine:preloadEffect(filename)
  166. end
  167. function audio.unloadSound(filename)
  168. if not filename then
  169. printError("audio.unloadSound() - invalid filename")
  170. return
  171. end
  172. if DEBUG > 1 then
  173. printf("[audio] unloadSound() - filename: %s", tostring(filename))
  174. end
  175. engine:unloadEffect(filename)
  176. end
  177. return audio