Cocos2d 播放音效

来源:互联网 发布:淘宝苹果充电器 编辑:程序博客网 时间:2024/04/30 21:02

Cocos2D对音乐/音效播放的封装已经很好了,下边是例子:

定义2个文件:

[cpp] view plaincopy
  1. #define MUSIC_FILE        "LOLBgMusic.mp3"  
  2. #define EFFECT_FILE      "Attack.wav"  

在初始化的时候预加载音乐/音效文件:

[cpp] view plaincopy
  1. //预加载  
  2.     CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);  
  3.     CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect( EFFECT_FILE );  
  4.     //设置音量  
  5.     CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(0.5);  
  6.     CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);  

播放:

[cpp] view plaincopy
  1. //循环播放背景音乐,  
  2. CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);  
[cpp] view plaincopy
  1. //播放音效  
  2. CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(EFFECT_FILE);  

其他相关函数:

[cpp] view plaincopy
  1. // stop background music  
  2. SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  3.  // pause background music  
  4.  SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  5. // resume background music  
  6. SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  7. // rewind background music  
  8. SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();  
  9. // is background music playing  
  10. SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()  
  11. // add bakcground music volume  
  12. SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(SimpleAudioEngine::sharedEngine()->getBackgroundMusicVolume() + 0.1f);  
  13.   
  14.   
  15. SimpleAudioEngine::sharedEngine()->pauseEffect(m_nSoundId);  
  16. SimpleAudioEngine::sharedEngine()->resumeEffect(m_nSoundId);  
  17. SimpleAudioEngine::sharedEngine()->pauseAllEffects();  
  18. SimpleAudioEngine::sharedEngine()->resumeAllEffects();  
  19. SimpleAudioEngine::sharedEngine()->stopAllEffects();  
0 0
原创粉丝点击