cocos2d-x 播放音乐和音效

首先在头文件里添加:

#include "SimpleAudioEngine.h"

 

然后就可以调用如下函数来初始化和播放音乐:

//预加载背景音乐
  CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( "Sound/SpaceGame.wav" );
  //播放背景音乐
  CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Sound/SpaceGame.wav");
  //预加载音效
  CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("Sound/explosion_large.mp3");
  //播放音效
 CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Sound/explosion_large.mp3");

你可能感兴趣的:(cocos2d-x 播放音乐和音效)