「Cocos2d-x」AudioEngineとSimpleAudioEngine
Cocos2d-x3.3beta0から「SimpleAudioEngine」がなくなり、
AudioEngineクラスが作られたので、導入方法をメモ。
バージョン3.4から大幅なAudio周りの修正が入っている。
現在の最新のバージョン3.6ならpreloadとかもなおっているかもね...。
AudioEngine
初めに、インクルードとネームスペース。
#include "audio/include/AudioEngine.h" using namespace cocos2d; using namespace experimental;
簡単機能の使い方
//Audioの全停止 AudioEngine::stopAll(); //AudioIDを指定してAudioの停止 sound_id = AudioEngine::play2d(SOUND_FILE_BGM, true, 0.5, nullptr);//ループON、音量:0.5 AudioEngine::stop(sound_id); //Audioの再生 AudioEngine::play2d(SOUND_FILE_TEXT_SE); //Audioを指定時間から再生 udioEngine::setCurrentTime(soundID, 4.0f);
その他の機能は以下のリファレンスを参照する
cocos2d-x: AudioEngine Class Reference
※追記
以下の2つの方法をメモ。
・アプリを起動中、途中でホーム画面に戻した際に、音楽をストップさせる。(applicationDidEnterBackground())
・また、逆に途中から再開する際に、音楽を再生させる。(applicationWillEnterForeground())
「AppDelegate.cpp」の”applicationDidEnterBackground()”と”applicationWillEnterForeground()”の中に追記する。
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); AudioEngine::pauseAll(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); AudioEngine::resumeAll(); }
さらに、追記(2017/06/02)
サウンドの推奨拡張子ついて
プラットフォームによって推奨されているサウンドの拡張子が異なる。
「wave」や「mp3」はAndroidとiOSのどちらのプラットフォームでも利用できる。しかし、どちらにも以下の欠点がある。
- 「wave」はファイル容量が大きい。
- 「mp3」はループ再生に不向き。
クロスプラットフォームの場合、理想的な方法は、コード内で切り替えるやり方がいい。
さらに、追記(2017/07/10)
エラー処理
「Fail to play %s cause by limited max instance of AudioEngine」
「Fail to play %s cause by limited max instance of AudioProfile」
「Fail to play %s cause by limited minimum delay」
ファイル場所:「...\cocos2d\cocos\audio\AudioEngine.cpp」
AudioPlayer (322): cocos2d::experimental::AudioPlayer::rotateBufferThread exited.
AudioPlayer (131): cocos2d::experimental::AudioPlayer::play2d:alSourcePlay error code:a004
原因:音が鳴り終わる前に、次の音を鳴らそうと準備する所でエラーが出る
参考サイト:
iOSアプリ開発:OpenALで音連続再生時にエラー発生 - Qiita
OpenAL 1.1 Specification and Reference
ファイル場所:「...\cocos2d\cocos\audio\win32\AudioPlayer.cpp」
SimpleAudioEngine
#include "audio/include/SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; //==================================================== // サウンド前読込み //==================================================== // BGM SimpleAudioEngine::getInstance()->preloadBackgroundMusic(SOUND_FILE_BGM); // 効果音 SimpleAudioEngine::getInstance()->preloadEffect(SOUND_FILE_SE_STARTUP); //==================================================== // サウンド再生 //==================================================== // BGM SimpleAudioEngine::getInstance()->playBackgroundMusic(SOUND_FILE_BGM_BOSS, true); // 効果音 SimpleAudioEngine::getInstance()->playEffect(SOUND_FILE_SE_STARTUP); // サウンドID設定して再生 soundID = SimpleAudioEngine::getInstance()->playEffect(SOUND_FILE_SE, true); //==================================================== // サウンド停止 //==================================================== // 全てのBGMを停止 SimpleAudioEngine::getInstance()->stopBackgroundMusic(); // 全ての効果音を停止 SimpleAudioEngine::getInstance()->stopAllEffects(); // サウンドIDのサウンド停止 SimpleAudioEngine::getInstance()->stopEffect(soundID);