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

48 篇文章 0 订阅
33 篇文章 0 订阅

https://blog.csdn.net/wwj_748/article/details/38588563

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


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


示例代码:

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值