环境:VS2012,cocos2d-x3.5,framework3.5,cocos studio 2.2.1
新建项目
一个场景,两个按钮,要实现功能,一个按钮按下播放音乐,另一个按下停止播放
两个文件完整代码
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "editor-support/cocostudio/CCSGUIReader.h"
using namespace cocos2d::ui;
using namespace cocostudio;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
virtual void playevent(Ref* ref, Widget::TouchEventType touchType);
virtual void stopevent(Ref* ref, Widget::TouchEventType touchType);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
// android effect only support ogg
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#define EFFECT_FILE "effect2.ogg"
#elif( CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
#define EFFECT_FILE "effect1.raw"
#else
#define EFFECT_FILE "effect1.wav"
#endif // CC_PLATFOR_ANDROID
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#define MUSIC_FILE "music.mid"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
#define MUSIC_FILE "background.wav"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX )
#define MUSIC_FILE "background.ogg"
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#define MUSIC_FILE "background.caf"
#else
#define MUSIC_FILE "background.mp3"
#endif // CC_PLATFOR_WIN32
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace CocosDenshion;
Scene* HelloWorld::createScene()
{
SimpleAudioEngine::getInstance()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::getInstance()->preloadEffect( EFFECT_FILE );
// set default volume
SimpleAudioEngine::getInstance()->setEffectsVolume(0.5);
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.5);
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
void HelloWorld::playevent(Ref* ref, Widget::TouchEventType touchType){
switch (touchType)
{
case cocos2d::ui::Widget::TouchEventType::BEGAN:
break;
case cocos2d::ui::Widget::TouchEventType::MOVED:
break;
case cocos2d::ui::Widget::TouchEventType::ENDED:
SimpleAudioEngine::getInstance()->playBackgroundMusic(MUSIC_FILE,true);
//SimpleAudioEngine::getInstance()->playEffect(EFFECT_FILE,false,1.0f,0.0f,1.0f);
break;
case cocos2d::ui::Widget::TouchEventType::CANCELED:
break;
default:
break;
}
}
void HelloWorld::stopevent(Ref* ref, Widget::TouchEventType touchType){
switch (touchType)
{
case cocos2d::ui::Widget::TouchEventType::BEGAN:
break;
case cocos2d::ui::Widget::TouchEventType::MOVED:
break;
case cocos2d::ui::Widget::TouchEventType::ENDED:
SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
break;
case cocos2d::ui::Widget::TouchEventType::CANCELED:
break;
default:
break;
}
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false;
}
// preload background music and effect
auto rootNode = CSLoader::createNode("MainScene.csb");
Button* playBtn=(Button*)(rootNode->getChildByName("play"));
Button* stopBtn=(Button*)(rootNode->getChildByName("stop"));
playBtn->addTouchEventListener(this,toucheventselector(HelloWorld::playevent));
stopBtn->addTouchEventListener(this,toucheventselector(HelloWorld::stopevent));
addChild(rootNode);
return true;
}
主要用到的是SimpleAudioEngine
在HelloWorldScene.h中加入
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
在HelloWorldScene.cpp中要预加载音频文件
用的是
SimpleAudioEngine::getInstance()->preloadBackgroundMusic( MUSIC_FILE );
SimpleAudioEngine::getInstance()->preloadEffect( EFFECT_FILE );
// set default volume
SimpleAudioEngine::getInstance()->setEffectsVolume(0.5);
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.5);
第一行预加载背景音乐,第二行加载音效
第四行设音效音量
第五行设音乐音量
我们设置按钮的点击事件,一个是播放
SimpleAudioEngine::getInstance()->playBackgroundMusic(MUSIC_FILE,true);
一个是停止
SimpleAudioEngine::getInstance()->stopBackgroundMusic(true);
OK,简单实现了
音效也类似
特别注意,一个问题卡了我很长很长的时间,实在太低级了,但有可能会卡住,特别记录下来
刚开始弄时,声音怎么也没有,最后才发现,问题出在声音文件存放的地方
我是在Studio里建的项目,导入了音频文件,然后发布到VS
这样一来,声音文件是在cocosstudio文件夹中
正确的存放位置是Resources文件夹,我把声音文件考过去之后,OK了
卡死我了