位置: 编程技术 - 正文
推荐整理分享cocos2dx AudioEngine初体验,音效播放结束判断测试,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
由于simpleAudioEngine在播放音效时无法获取状态也无法在播放完成后回调,造成音效在按键频繁时发声重叠。改mac版本的库对目前的我来说挑战难度太大。
于是百度了半天,发现3.6版本的cocos2dx有了新的声音播放库AudioEngine。于是体验下,感觉还不错:
直接贴代码:
头文件:
#ifndef __macstudycocos2dx__newAudioEngineTest__
#define __macstudycocos2dx__newAudioEngineTest__
#include <stdio.h>
#include "cocos2d.h"
#include "audio/include/AudioEngine.h"
USING_NS_CC;
using namespace experimental;
class newAudioEngineTest:publicLayer ,publicAudioEngine{
public:
staticcocos2d::Scene* createScene();
virtualbool init();
// a selector callback
void playCallback(cocos2d::Ref* pSender);// 点击按钮后开始播放的回调函数
CREATE_FUNC(newAudioEngineTest);
bool isplaying=false;//标记播放状态
void changer_playingstatus();//播放完成后的回调函数,用来更改播放状态
};
newAudioEngineTest.cpp如下:
#endif /* defined(__macstudycocos2dx__newAudioEngineTest__) */
//
// newAudioEngineTest.cpp
// macstudycocos2dx
//
// Created by WangWei on /6/.
//
//
#include "newAudioEngineTest.h"
Scene* newAudioEngineTest::createScene()
{
// 'scene' is an autorelease object
auto scene =Scene::create();
// 'layer' is an autorelease object
auto layer =newAudioEngineTest::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool newAudioEngineTest::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize =Director::getInstance()->getVisibleSize();
Vec2 origin =Director::getInstance()->getVisibleOrigin();
auto menuitem=MenuItemFont::create("play wav", CC_CALLBACK_1(newAudioEngineTest::playCallback,this));
//创建菜单项,点击时播放音效
menuitem->setPosition(Vec2(0,0));
// create menu, it's an autorelease object
auto menu =Menu::create(menuitem,NULL);
menu->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(menu,1);
return true;
}
voidnewAudioEngineTest::playCallback(Ref* pSender)
{
if (!isplaying) {
log("wav is not playing!");
isplaying=true;
int audioID=play2d("man1.wav");//播放音效文件
log("audioID =%d",audioID);
setFinishCallback(audioID,CC_CALLBACK_0(newAudioEngineTest::changer_playingstatus,this));
//音效播放完成后修改播放状态,此时会用到audioID
}
else{
log("wav is playing,this click is not response!");
}
}
voidnewAudioEngineTest::changer_playingstatus(){
isplaying=false;
log("finished,status has been change");
}
以下是log输出:(可以看出audioID是改变的,即使播放同一个音效)
-- ::. macstudycocos2dx iOS[:] cocos2d: surface size: x
wav is not playing!
audioID =0
finished,status has been change
wav is not playing!
audioID =1
finished,status has been change
wav is not playing!
audioID =2
finished,status has been change
wav is not playing!
audioID =3
finished,status has been change
wav is not playing!
audioID =4
wav is playing,this click is not response! //快速点击时
finished,status has been change
wav is not playing!
audioID =5
wav is playing,this click is not response! //快速点击时
wav is playing,this click is not response! //快速点击时
finished,status has been change
wav is not playing!
audioID =6
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
finished,status has been change
wav is not playing!
audioID =7
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
wav is playing,this click is not response!
finished,status has been change
个人觉得如果音效可以使用AudioEngine来做,记住要用caf和wav式。背景音乐可以使用simpleAudioEngine来播放,毕竟simpleAudioEngine有缓存机制。
如果不继承类时可以使用以下方式来播放哦:
int audioID=experimental::AudioEngine::play2d(wavfilestring->getCString());
版权声明:本文为博主原创文章,未经博主允许不得转载。
Cocos2d-3.x_在Lua中使用cjson库解析json prename=codeclass=cppboolAppDelegate::applicationDidFinishLaunching(){autoengine=LuaEngine::getInstance();//注册cjsonLuaStack*stack=engine-getLuaStack();luaopen_lua_extensions_cjson(stack-getLuaStat
实现了个类似blood brothers中的转轴特效 最近比较忙,有一段时间没写东西了,今天算是忙里偷闲,分享点东西。在美区有个游戏叫bloodbrothers,玩了玩,感觉还不错,其中有一个页面,可以选
在cocos2dx中实现水波滤镜 因为工作原因,开始转向cocos2d-x开发方向了。自然的,凭着之前引擎的经验偏向底层渲染研究。在此期间看了两本书《cocos2d-x权威指南》《cocos2d-x高级
标签: cocos2dx AudioEngine初体验,音效播放结束判断测试
本文链接地址:https://www.jiuchutong.com/biancheng/368363.html 转载请保留说明!上一篇:cocos2dx 使用 crypto++(cocos2d解密)
友情链接: 武汉网站建设