Cocos2d-x|Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)

本篇博客介绍Cocos2d-x 3.2中Lua示例的音频测试,Cocos2d-x使用SimpleAudioEngine这个类来实现音频的控制,比如播放、暂停、停止等操作。
Lua代码中,使用的是AudioEngine,具体实现可以参考AudioEngine.lua文件,只是把SimpleAudioEngin进行了封装。
Cocos2d-x|Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)
文章图片

示例代码:

【Cocos2d-x|Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)】

[javascript] view plain copy print ?

  1. --[[
  2. CocosDenshionTest.lua
  3. Cocos2d-x 音频支持
  4. ]]--
  5. require "AudioEngine"
  6. local EFFECT_FILE = "effect1.wav"
  7. local MUSIC_FILE = nil
  8. -- 获取目标平台
  9. local targetPlatform = cc.Application:getInstance():getTargetPlatform()
  10. -- iphone或者ipad
  11. if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then
  12. MUSIC_FILE = "background.caf" -- caf格式
  13. else
  14. MUSIC_FILE = "background.mp3" -- mp3格式
  15. end
  16. local LINE_SPACE = 40
  17. local function CocosDenshionTest()
  18. local ret = cc.Layer:create()
  19. local m_pItmeMenu = nil
  20. local m_tBeginPos = cc.p(0, 0)
  21. local m_nSoundId = 0
  22. -- 测试菜单项
  23. local testItems = {
  24. "play background music",
  25. "stop background music",
  26. "pause background music",
  27. "resume background music",
  28. "rewind background music",
  29. "is background music playing",
  30. "play effect",
  31. "play effect repeatly",
  32. "stop effect",
  33. "unload effect",
  34. "add background music volume",
  35. "sub background music volume",
  36. "add effects volume",
  37. "sub effects volume",
  38. "pause effect",
  39. "resume effect",
  40. "pause all effects",
  41. "resume all effects",
  42. "stop all effects"
  43. }
  44. -- 菜单回调方法
  45. local function menuCallback(tag, pMenuItem)
  46. local nIdx = pMenuItem:getLocalZOrder() - 10000
  47. -- play background music
  48. if nIdx ==0 then
  49. AudioEngine.playMusic(MUSIC_FILE, true) -- 播放音乐
  50. elseif nIdx == 1 then
  51. -- stop background music
  52. AudioEngine.stopMusic()-- 停止背景音乐
  53. elseif nIdx == 2 then
  54. -- pause background music
  55. AudioEngine.pauseMusic() -- 暂停音乐
  56. elseif nIdx == 3 then
  57. -- resume background music
  58. AudioEngine.resumeMusic() -- 继续播放音乐
  59. -- rewind background music
  60. elseif nIdx == 4 then
  61. AudioEngine.rewindMusic()-- 循环播放
  62. elseif nIdx == 5 then
  63. -- is background music playing
  64. if AudioEngine.isMusicPlaying () then -- 音乐正在播放
  65. cclog("background music is playing")
  66. else
  67. cclog("background music is not playing")
  68. end
  69. elseif nIdx == 6 then
  70. -- play effect
  71. m_nSoundId = AudioEngine.playEffect(EFFECT_FILE)-- 播放音效
  72. elseif nIdx == 7 then
  73. -- play effect
  74. m_nSoundId = AudioEngine.playEffect(EFFECT_FILE, true) -- 播放音效,第二个参数表示是否循环,true表示循环
  75. elseif nIdx == 8 then
  76. -- stop effect
  77. AudioEngine.stopEffect(m_nSoundId) -- 停止音效
  78. elseif nIdx == 9 then
  79. -- unload effect
  80. AudioEngine.unloadEffect(EFFECT_FILE)-- 不加载音效
  81. elseif nIdx == 10 then
  82. -- add bakcground music volume
  83. AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() + 0.1) -- 增加音量
  84. elseif nIdx == 11 then
  85. -- sub backgroud music volume
  86. AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() - 0.1) -- 减小音量
  87. elseif nIdx == 12 then
  88. -- add effects volume
  89. AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() + 0.1) -- 增加音效音量
  90. elseif nIdx == 13 then
  91. -- sub effects volume
  92. AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() - 0.1) -- 减少音效音量
  93. elseif nIdx == 14 then
  94. AudioEngine.pauseEffect(m_nSoundId)-- 暂停音效
  95. elseif nIdx == 15 then
  96. AudioEngine.resumeEffect(m_nSoundId) -- 恢复音效
  97. elseif nIdx == 16 then
  98. AudioEngine.pauseAllEffects() -- 暂停所有音效
  99. elseif nIdx == 17 then
  100. AudioEngine.resumeAllEffects() -- 恢复所有音效
  101. elseif nIdx == 18 then
  102. AudioEngine.stopAllEffects() -- 停止所有音效
  103. end
  104. end
  105. -- add menu items for tests
  106. m_pItmeMenu = cc.Menu:create() -- 创建菜单
  107. m_nTestCount = table.getn(testItems)
  108. local i = 1
  109. fori = 1, m_nTestCount do
  110. locallabel = cc.Label:createWithTTF(testItems[i], s_arialPath, 24)
  111. label:setAnchorPoint(cc.p(0.5, 0.5))
  112. localpMenuItem = cc.MenuItemLabel:create(label) -- 菜单标签
  113. pMenuItem:registerScriptTapHandler(menuCallback) -- 注册菜单回调方法
  114. m_pItmeMenu:addChild(pMenuItem, i + 10000 -1)
  115. pMenuItem:setPosition( cc.p( VisibleRect:center().x, (VisibleRect:top().y - i * LINE_SPACE) ))
  116. end
  117. -- 设置菜单内容大小
  118. m_pItmeMenu:setContentSize(cc.size(VisibleRect:getVisibleRect().width, (m_nTestCount + 1) * LINE_SPACE))
  119. m_pItmeMenu:setPosition(cc.p(0, 0))
  120. ret:addChild(m_pItmeMenu)
  121. -- preload background music and effect
  122. AudioEngine.preloadMusic( MUSIC_FILE ) -- 预加载音乐
  123. AudioEngine.preloadEffect( EFFECT_FILE ) -- 预加载音效
  124. -- set default volume
  125. AudioEngine.setEffectsVolume(0.5) -- 设置音效音量
  126. AudioEngine.setMusicVolume(0.5) -- 设置音乐音量
  127. local function onNodeEvent(event)
  128. if event == "enter" then -- 进来时
  129. elseif event == "exit" then --退出时
  130. AudioEngine.destroyInstance() -- 销毁对象
  131. end
  132. end
  133. -- 注册层的结点事件
  134. ret:registerScriptHandler(onNodeEvent)
  135. local prev = {x = 0, y = 0}
  136. local function onTouchEvent(eventType, x, y)
  137. if eventType == "began" then -- 开始点击
  138. prev.x = x
  139. prev.y = y
  140. m_tBeginPos = cc.p(x, y) -- 开始点击位置
  141. return true
  142. elseifeventType == "moved" then -- 移动事件
  143. local touchLocation = cc.p(x, y) -- 获取触摸的位置
  144. local nMoveY = touchLocation.y - m_tBeginPos.y -- 触摸位置减去开始位置等于移动的距离
  145. local curPosX, curPosY = m_pItmeMenu:getPosition() -- 获取当前菜单的位置
  146. local curPos = cc.p(curPosX, curPosY) --当前位置
  147. local nextPos = cc.p(curPos.x, curPos.y + nMoveY) -- 下一个位置
  148. if nextPos.y < 0.0 then
  149. m_pItmeMenu:setPosition(cc.p(0, 0))
  150. end
  151. if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then
  152. m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))
  153. end
  154. m_pItmeMenu:setPosition(nextPos)
  155. m_tBeginPos.x = touchLocation.x-- 重新记录开始位置
  156. m_tBeginPos.y = touchLocation.y
  157. prev.x = x
  158. prev.y = y
  159. end
  160. end
  161. -- 触摸开始回调方法
  162. local function onTouchBegan(touch, event)
  163. local location = touch:getLocation()
  164. prev.x = location.x
  165. prev.y = location.y
  166. m_tBeginPos = location
  167. return true
  168. end
  169. -- 触摸移动的回调方法
  170. local function onTouchMoved(touch, event)
  171. local location = touch:getLocation()
  172. local touchLocation = location
  173. local nMoveY = touchLocation.y - m_tBeginPos.y
  174. local curPosX, curPosY = m_pItmeMenu:getPosition()
  175. local curPos = cc.p(curPosX, curPosY)
  176. local nextPos = cc.p(curPos.x, curPos.y + nMoveY)
  177. if nextPos.y < 0.0 then
  178. m_pItmeMenu:setPosition(cc.p(0, 0))
  179. end
  180. if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then
  181. m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))
  182. end
  183. m_pItmeMenu:setPosition(nextPos)
  184. m_tBeginPos.x = touchLocation.x
  185. m_tBeginPos.y = touchLocation.y
  186. prev.x = location.x
  187. prev.y = location.y
  188. end
  189. -- 单点触摸
  190. local listener = cc.EventListenerTouchOneByOne:create()
  191. listener:setSwallowTouches(true)
  192. -- 注册脚本监听事件
  193. listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
  194. listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
  195. local eventDispatcher = ret:getEventDispatcher()
  196. eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret)
  197. return ret
  198. end
  199. function CocosDenshionTestMain()
  200. cclog("CocosDenshionTestMain")
  201. local scene = cc.Scene:create()
  202. scene:addChild(CocosDenshionTest())
  203. scene:addChild(CreateBackMenuItem())
  204. return scene
  205. end

    推荐阅读