LoginSignup
6
5

More than 5 years have passed since last update.

cocos2d-xにおけるAudioEngineの問題

Last updated at Posted at 2016-01-17

cocos2d-xでSimpleAudioEngineでできないことがあったとき、AudioEngineを使う事が多いでしょう。そこで、ちょっとした問題に遭遇したため備忘録として書いておきます。
今回、AudioEngineを使おうとして、
https://korosame.wordpress.com/2014/10/28/【cocos2d-x】simpleaudioengineではなくaudioengineを使う/
を参考にしたけれど音声が再生されなかったため原因を探すためcocosのAudioEngineのコードを見たところ、

AudioEngine-inl.mm
bool AudioEngineImpl::init()
{
    bool ret = false;
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
        s_AudioEngineSessionHandler = [[AudioEngineSessionHandler alloc] init];
#endif
    do{
        s_ALDevice = alcOpenDevice(NULL);        
if (s_ALDevice) {
            auto alError = alGetError();
            s_ALContext = alcCreateContext(s_ALDevice, nullptr);
            alcMakeContextCurrent(s_ALContext);

            alGenSources(MAX_AUDIOINSTANCES, _alSources);
            alError = alGetError();
            if(alError != AL_NO_ERROR)
            {
                printf("%s:generating sources fail! error = %x\n", __PRETTY_FUNCTION__, alError);
                break;
            }

            for (int i = 0; i < MAX_AUDIOINSTANCES; ++i) {
                _alSourceUsed[_alSources[i]] = false;
            }

            _threadPool = new (std::nothrow) AudioEngineThreadPool();
            ret = true;
        }
    }while (false);

    return ret;
}

のところでalcOpenDevice(NULL)でNULLを返しているのが原因と分かったためOpenALで問題になっていました。
http://stackoverflow.com/questions/24067201/getting-an-audio-device-with-openal
を調べたところOpenALとAppleのAPIにちょっと問題があるらしく、alcOpenDeviceを使う前に、何かAPIを使って呼び出しているとNULLを返すらしい。
コードを見返したところSimpleAudioEngineを前のシーンで使っていてそれで問題があったみたいです。
SimpleAudioEngineで書いてたところをすべてAudioEngineにしたらうまくいくため、今後同じ事があったら気を付けてください。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5