Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)
 本篇博客介绍Cocos2d-x 3.2中Lua演示样例的音频測试。Cocos2d-x使用SimpleAudioEngine这个类来实现音频的控制,比方播放、暂停、停止等操作。

Lua代码中。使用的是AudioEngine,详细实现能够參考AudioEngine.lua文件。仅仅是把SimpleAudioEngin进行了封装。
演示样例代码:
--[[
CocosDenshionTest.lua
Cocos2d-x 音频支持
]]--
require "AudioEngine"
local EFFECT_FILE = "effect1.wav" local MUSIC_FILE = nil
-- 获取目标平台
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
-- iphone或者ipad
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then
MUSIC_FILE = "background.caf" -- caf格式
else
MUSIC_FILE = "background.mp3" -- mp3格式
end local LINE_SPACE = 40 local function CocosDenshionTest()
local ret = cc.Layer:create()
local m_pItmeMenu = nil
local m_tBeginPos = cc.p(0, 0)
local m_nSoundId = 0 -- 測试菜单项
local testItems = {
"play background music",
"stop background music",
"pause background music",
"resume background music",
"rewind background music",
"is background music playing",
"play effect",
"play effect repeatly",
"stop effect",
"unload effect",
"add background music volume",
"sub background music volume",
"add effects volume",
"sub effects volume",
"pause effect",
"resume effect",
"pause all effects",
"resume all effects",
"stop all effects"
} -- 菜单回调方法
local function menuCallback(tag, pMenuItem)
local nIdx = pMenuItem:getLocalZOrder() - 10000
-- play background music
if nIdx == 0 then
AudioEngine.playMusic(MUSIC_FILE, true) -- 播放音乐
elseif nIdx == 1 then
-- stop background music
AudioEngine.stopMusic() -- 停止背景音乐
elseif nIdx == 2 then
-- pause background music
AudioEngine.pauseMusic() -- 暂停音乐
elseif nIdx == 3 then
-- resume background music
AudioEngine.resumeMusic() -- 继续播放音乐
-- rewind background music
elseif nIdx == 4 then
AudioEngine.rewindMusic() -- 循环播放
elseif nIdx == 5 then
-- is background music playing
if AudioEngine.isMusicPlaying () then -- 音乐正在播放
cclog("background music is playing")
else
cclog("background music is not playing")
end
elseif nIdx == 6 then
-- play effect
m_nSoundId = AudioEngine.playEffect(EFFECT_FILE) -- 播放音效
elseif nIdx == 7 then
-- play effect
m_nSoundId = AudioEngine.playEffect(EFFECT_FILE, true) -- 播放音效,第二个參数表示是否循环,true表示循环
elseif nIdx == 8 then
-- stop effect
AudioEngine.stopEffect(m_nSoundId) -- 停止音效
elseif nIdx == 9 then
-- unload effect
AudioEngine.unloadEffect(EFFECT_FILE) -- 不载入音效
elseif nIdx == 10 then
-- add bakcground music volume
AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() + 0.1) -- 添加音量
elseif nIdx == 11 then
-- sub backgroud music volume
AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() - 0.1) -- 减小音量
elseif nIdx == 12 then
-- add effects volume
AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() + 0.1) -- 添加音效音量
elseif nIdx == 13 then
-- sub effects volume
AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() - 0.1) -- 降低音效音量
elseif nIdx == 14 then
AudioEngine.pauseEffect(m_nSoundId) -- 暂停音效
elseif nIdx == 15 then
AudioEngine.resumeEffect(m_nSoundId) -- 恢复音效
elseif nIdx == 16 then
AudioEngine.pauseAllEffects() -- 暂停全部音效
elseif nIdx == 17 then
AudioEngine.resumeAllEffects() -- 恢复全部音效
elseif nIdx == 18 then
AudioEngine.stopAllEffects() -- 停止全部音效
end
end
-- add menu items for tests
m_pItmeMenu = cc.Menu:create() -- 创建菜单 m_nTestCount = table.getn(testItems)
local i = 1
for i = 1, m_nTestCount do
local label = cc.Label:createWithTTF(testItems[i], s_arialPath, 24)
label:setAnchorPoint(cc.p(0.5, 0.5))
local pMenuItem = cc.MenuItemLabel:create(label) -- 菜单标签
pMenuItem:registerScriptTapHandler(menuCallback) -- 注冊菜单回调方法
m_pItmeMenu:addChild(pMenuItem, i + 10000 -1)
pMenuItem:setPosition( cc.p( VisibleRect:center().x, (VisibleRect:top().y - i * LINE_SPACE) ))
end -- 设置菜单内容大小
m_pItmeMenu:setContentSize(cc.size(VisibleRect:getVisibleRect().width, (m_nTestCount + 1) * LINE_SPACE))
m_pItmeMenu:setPosition(cc.p(0, 0))
ret:addChild(m_pItmeMenu) -- preload background music and effect
AudioEngine.preloadMusic( MUSIC_FILE ) -- 预载入音乐
AudioEngine.preloadEffect( EFFECT_FILE ) -- 预载入音效 -- set default volume
AudioEngine.setEffectsVolume(0.5) -- 设置音效音量
AudioEngine.setMusicVolume(0.5) -- 设置音乐音量 local function onNodeEvent(event)
if event == "enter" then -- 进来时 elseif event == "exit" then -- 退出时
AudioEngine.destroyInstance() -- 销毁对象
end
end -- 注冊层的结点事件
ret:registerScriptHandler(onNodeEvent) local prev = {x = 0, y = 0}
local function onTouchEvent(eventType, x, y)
if eventType == "began" then -- 開始点击
prev.x = x
prev.y = y
m_tBeginPos = cc.p(x, y) -- 開始点击位置
return true
elseif eventType == "moved" then -- 移动事件
local touchLocation = cc.p(x, y) -- 获取触摸的位置
local nMoveY = touchLocation.y - m_tBeginPos.y -- 触摸位置减去開始位置等于移动的距离
local curPosX, curPosY = m_pItmeMenu:getPosition() -- 获取当前菜单的位置
local curPos = cc.p(curPosX, curPosY) -- 当前位置
local nextPos = cc.p(curPos.x, curPos.y + nMoveY) -- 下一个位置 if nextPos.y < 0.0 then
m_pItmeMenu:setPosition(cc.p(0, 0))
end if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then
m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))
end m_pItmeMenu:setPosition(nextPos)
m_tBeginPos.x = touchLocation.x -- 又一次记录開始位置
m_tBeginPos.y = touchLocation.y prev.x = x
prev.y = y
end
end -- 触摸開始回调方法
local function onTouchBegan(touch, event)
local location = touch:getLocation()
prev.x = location.x
prev.y = location.y
m_tBeginPos = location
return true
end -- 触摸移动的回调方法
local function onTouchMoved(touch, event)
local location = touch:getLocation()
local touchLocation = location
local nMoveY = touchLocation.y - m_tBeginPos.y
local curPosX, curPosY = m_pItmeMenu:getPosition()
local curPos = cc.p(curPosX, curPosY)
local nextPos = cc.p(curPos.x, curPos.y + nMoveY) if nextPos.y < 0.0 then
m_pItmeMenu:setPosition(cc.p(0, 0))
end if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then
m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))
end m_pItmeMenu:setPosition(nextPos)
m_tBeginPos.x = touchLocation.x
m_tBeginPos.y = touchLocation.y prev.x = location.x
prev.y = location.y
end -- 单点触摸
local listener = cc.EventListenerTouchOneByOne:create()
listener:setSwallowTouches(true)
-- 注冊脚本监听事件
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
local eventDispatcher = ret:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) return ret
end function CocosDenshionTestMain()
cclog("CocosDenshionTestMain")
local scene = cc.Scene:create()
scene:addChild(CocosDenshionTest())
scene:addChild(CreateBackMenuItem())
return scene
end

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)的更多相关文章

  1. Cocos2d-x 3.2 Lua演示样本 ActionTest(操作测试)

    Cocos2d-x 3.2 Lua演示样本 ActionTest(操作测试) 2014年博文大赛,请投上您宝贵的一票:http://vote.blog.csdn.net/Article/Details ...

  2. Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)

    Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理) 本篇博客介绍Cocos2d-x的动作管理样例,这个样例展示了Cocos2d-x的几个动作: MoveTo ...

  3. Cocos2d-x 3.1.1 Lua演示样例 ActionEaseTest(动作)

    Cocos2d-x Lua演示样例 ActionEaseTest(动作)   本篇博客介绍Cocos2d-x中的动作,Cocos2d-x为我们提供了丰富的动作接口,以下笔者就具体介绍一下:   本系列 ...

  4. 模式识别 - 处理多个演示样本研究(MIL)特点(matlab)

    处理多个演示样本研究(MIL)特点(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习 ...

  5. Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)

    Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)  本篇博客介绍Cocos2d-x 3.2Lua演示样例中点击移动的样例,在这个样例你能够得到怎样创建单点触 ...

  6. Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)

    Cocos2d-x 3.2 Lua演示样例 XMLHttpRequestTest(Http网络请求)     本篇博客介绍Cocos2d-x 3.2Lua演示样例中的XMLHttpRequestTes ...

  7. java注意事项演示 地图产生表 演示样本 来自thinking in java 4 20代码的章

    java注意事项演示 地图产生表 演示样本  来自thinking in java 4 20代码的章 thinking in java 4免费下载:http://download.csdn.net/d ...

  8. Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器)

    Cocos2d-x 3.2 Lua演示样例 AssetsManagerTest(资源管理器) 本篇博客介绍Cocos2d-x 为我们提供的一个类--AssetsManager在Lua中的使用样例,效果 ...

  9. Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条)

    Cocos2d-x 3.1.1 Lua演示样例 ActionsProgressTest(进度条) 本篇博客介绍Cocos2d-x中的进度条动画,进度条涉及以下几个重要的类和方法,笔者来给大家具体解说一 ...

随机推荐

  1. Javascript异步数据的同步处理方法

    数据处理方法封装 var DataWatch=(function(){ var gWatch={},cursor= 0,callback_key = 'callback',gMap={}; var c ...

  2. Spring Data Redis—Pub/Sub(附Web项目源码) (转)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...

  3. ECSHOP分类页面筛选功能(按分类下子分类和品牌筛选)

    其实分类页面里面本来就有相关的品牌.属性.分类的筛选功能在category.php和模板加上相应的功能即可 1.读出当前分类的所有下级分类 $chlidren_category = $GLOBALS[ ...

  4. 在阿里云上布置git server

    前言 东莞,晴,26至32度. 一直以为都是使用SVN Server作为私用的版本号控制器.随着Git的大行其道.近期由于项目须要,也试着在阿里云上部署Git Server.这里由于团队人员少.我採用 ...

  5. M3U8格式解说及实际应用分析

    M3U8有啥优点 ? 网上搜索了一下,大家众说纷纭,个人理解主要是能够做多码率的适配,依据网络带宽,client会选择一个适合自己码率的文件进行播放,保证视频流的流畅. 在IOS device和mac ...

  6. struts2集成fckeditor(来自大型门户网站是这样练成的一书)

  7. uva11600 状压期望dp

    一般的期望dp是, dp[i] = dp[j] * p[j] + 1; 即走到下一步需要1的时间,然后加上 下一步走到目标的期望*这一步走到下一步的概率 这一题,我们将联通分块缩为一个点,因为联通块都 ...

  8. WPF 数字小键盘Themes

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  9. 使用MVC写模式jsp连接到数据库操作

    首先用一个JavaBean封装数据库操作,即mvc中的模型 JdbcBean.java package data; import java.sql.*; public class JdbcBean { ...

  10. 在JAVA中使用LUA脚本记,javaj调用lua脚本的函数(转)

    最近在做一些奇怪的东西,需要Java应用能够接受用户提交的脚本并执行,网络部分我选择了NanoHTTPD提供基本的HTTP服务器支持,并在Java能承载的许多脚本语言中选择了很久,比如Rhino,Jy ...