고급 사운드 기능
구성
모바일 기기에선 게임에 특별한 상황이 있을수 있다.
예를들면 게임앱을 잠깐 백그라운드로 내렸다가 돌아오는경우,게임을 하는도중 전화가 올경우 전화가 끝나면 게임을 계속 해야 하기 때문에 이런상황들은 사운드제어 해야 한다.
다행인것은 게임엔진을 제작할때 이미 이런 상황을 고려해서 설계되었다는것이다.
AppDelegate.cpp 중에 아래와 같은 몇가지 방법이 있다.
C++
// This function will be called when the app is inactive. When comes a phone call,
// it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
위에 주석된 코드가 보입니까?만약 당신이 SimpleAudioEngine 사용하여 게임중에 사운드를 재생할때 이 주석처리한 코드를 활성화해줘야 한다.
그래야 방금 말한 상황을 해결할수 있다.
Pre-loading
음악파일이나 효과음파일을 로딩하는것은 시간은 잡아먹는 과정이다.
로딩으로 발생하는 게임중에 지연시간을 없애기 위해서 재생하기 전에 음악파일을 pre-loading 하면 된다.
C++
#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");
음량 제어
아래와 같이 코드를 통해서 음악과 효과음의 음량을 제어할수 있다:
C++
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
// setting the volume specifying value as a float
audio->setEffectsVolume(5.0f);
출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/audio/advanced.html>
Are your audio needs more advanced?
This far we have just talked about SimpleAudioEngine. For most games, SimpleAudioEngine provides all the functionality that they need. If your audio needs move beyond what SimpleAudioEngine can provide, Cocos2d-x also offers a second choice called just AudioEngine.
SimpleAudio Engine versus Audio Engine
출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/audio/engines.html>
'Cocos2d-x v3.17 > 고급 기능' 카테고리의 다른 글
[Cocos2d-x 고급기능]네트워크 액세스 (0) | 2018.08.11 |
---|---|
[Cocos2d-x 고급기능]파일 시스템 접근 (0) | 2018.08.11 |
[Cocos2d-x 고급기능]소리 제어 (0) | 2018.08.11 |
[Cocos2d-x 고급기능]배경 음악 재생 (0) | 2018.08.11 |
[Cocos2d-x 고급기능]음악과 효과음 (0) | 2018.08.11 |