zoukankan      html  css  js  c++  java
  • Cocos2d-x学习笔记(二十九)之 声音

      Cocos2D-x中采用SimpleAudioEngine类来实现跨平台的声音引擎。

      1、Cocos2D-x在不同平台下支持的背景音乐格式如下:

      android:android.media.MediaPlayer类支持的格式,包括MP3、WAV和3GP。

      IOS:Cocos2D-iPhone中CocosDenshion支持的类型,推荐MP3和CAF。

      Win32:MID和WAV。

      2、Cocos2D-x在不同平台下支持的音效格式如下:

      android:对于OGG格式支持最好,同时支持WAV格式。

      IOS:Cocos2D-iPhone中CocosDenshion支持的类型、推荐CAF。

      Win32:MID和WAV。

      3、SimpleAudioEngine类的主要成员函数:

      preloadBackgroundMusic:预加载背景音乐。

      playBackgroundMusic:播放背景音乐。

      stopBackgroundMusic:停止背景音乐。

      pauseBackgroundMusic:暂停背景音乐。

      resumeBackgroundMusic:重新开始背景音乐。

      rewindBackgroundMusic:回放背景音乐。

      willPlayBackgroundMusic:是否会播放背景音乐。

      isBackgroundMusicPlaying:是否正播放背景音乐。

      getBackgroundMusicVolume:获得背景音乐音量。

      setBackgroundMusicVolume:设置背景音乐音量。

      getEffectsVolume:获得音效音量。

      setEffectsVolume:设置音效音量。

      playEffect:播放音效,参数为文件路径和是否循环。

      pauseEffect:暂停音效,参数为播放时获得的ID号。

      pauseAllEffects:暂停所有音效。

      resumeEffects:开始音效,参数为播放时获得的ID号。

      resumeAllEffects:开始所有音效。

      stopEffect:停止音效,参数为播放时获得的ID号。

      stopAllEffects:停止所有音效。

      preloadEffect:预加载音效。

      unloadEffect:将预加载的音效从缓存中删除。

      SimpleAudioEngine类的使用示例如下:

    1 //预加载音乐和音效
    2 SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(MUSIC_FILE) );
    3 SimpleAudioEngine::sharedEngine()->preloadEffect( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath( EFFECT_FILE ) );
    4 
    5 //设置音乐和音效的音量
    6 SimpleAudioEngine::sharedEngine()->setEffectsVolume(  0.5 );
    7 SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume( 0.5 );

      注:预加载声音会使得程序在游戏中的执行效率提高,但是这样会增加内存的占用。在实际使用的时候要根据项目特点来达到效率和内存压力的平衡。

      

     1 //播放背景音乐
     2 SimpleAudioEngine::sharedEngine()->playBackgroundMusic( std::string( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath( MUSIC_FILE )).c_str() , true );
     3 //停止背景音乐
     4 SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
     5 //暂停背景音乐
     6 SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
     7 
     8 
     9 //播放音效,不循环播放
    10 m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect( std::string( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE) ).c_str() );
    11 //播放音效,循环播放
    12 m_nSoundId = SimpleAudioEngine::sharedEngine()->playEffect( std::string( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE) ).c_str(), true );
    13 //停止音效播放
    14 SimpleAudioEngine::sharedEngine()->stopEffect(m_nSoundId);
    15 //从缓存中删除音效
    16 SimpleAudioEngine::sharedEngine()->unloadEffect( std::string( CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(EFFECT_FILE) ).c_str() );
  • 相关阅读:
    UML用例图
    Google Map API 文档
    chrome 使用各种搜索引擎的方法 GIS
    javaScript 获得触发事件的元素 支持IE FireFox GIS
    html 的路径 GIS
    JavaScript prototype GIS
    chromium 的资源管理 的grit GIS
    [原]使用ucenter最土团购整合DX2bbs的心得
    [转]sqlserver日期函数
    最土Ajax实现/json
  • 原文地址:https://www.cnblogs.com/atong/p/3285655.html
Copyright © 2011-2022 走看看