IT培訓(xùn)網(wǎng)
IT在線學(xué)習(xí)
簡介
Cocos2d-x 提供了一個 SimpleAudioEngine 類支持游戲內(nèi)的音樂和音效。它可以被用來增加背景音樂,控制游戲音效。
SimpleAudioEngine 是一個共享的單例對象,你可以在代碼中的任何地方通過很簡單的方式獲取到。以下,我們會盡可能的為你展示它的各種使用方法。先來了解一下支持的文件格式。
支持的音樂格式:
支持的音效格式:
播放背景音樂
通過下面的方式,播放一個音頻文件作為背景音樂,可以控制背景音樂是否循環(huán)播放:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// set the background music and continuously play it.
audio->playBackgroundMusic("mymusic.mp3", true);
// set the background music and play it just once.
audio->playBackgroundMusic("mymusic.mp3", false);
播放音效
通過下面的方式,將一個音頻文件作為音效:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// play a sound effect, just once.
audio->playEffect("myEffect.mp3", false, 1.0f, 1.0f, 1.0f);
聲音控制
開始播放音樂和音效后,你可能需要對它們進(jìn)行一些控制,比如暫停、停止、恢復(fù)。這很容易完成,下面介紹:
1.暫停聲音:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// pause background music.
audio->pauseBackgroundMusic();
// pause a sound effect.
audio->pauseEffect();
// pause all sound effects.
audio->pauseAllEffects();
2.停止聲音:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// stop background music.
audio->stopBackgroundMusic();
// stop a sound effect.
audio->stopEffect();
// stops all running sound effects.
audio->stopAllEffects();
3.恢復(fù)聲音:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// resume background music.
audio->resumeBackgroundMusic();
// resume a sound effect.
audio->resumeEffect();
// resume all sound effects.
audio->resumeAllEffects();
預(yù)加載
加載音樂和音效通常是個耗時間的過程,為了防止由加載產(chǎn)生的延時導(dǎo)致實(shí)際播放與游戲播放不協(xié)調(diào)的現(xiàn)象,在播放音樂和音效前,可以預(yù)加載音樂文件:
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// pre-loading background music and effects. You could pre-load// effects, perhaps on app startup so they are already loaded// when you want to use them.
audio->preloadBackgroundMusic("myMusic1.mp3");
audio->preloadBackgroundMusic("myMusic2.mp3");
audio->preloadEffect("myEffect1.mp3");
audio->preloadEffect("myEffect2.mp3");
// unload a sound from cache. If you are finished with a sound and// you wont use it anymore in your game. unload it to free up// resources.
audio->unloadEffect("myEffect1.mp3");
>>本文地址:http://www.yceu.cn/zhuanye/2021/68693.html
聲明:本站稿件版權(quán)均屬中公教育優(yōu)就業(yè)所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
1 您的年齡
2 您的學(xué)歷
3 您更想做哪個方向的工作?