diff --git a/cocos/audio/CMakeLists.txt b/cocos/audio/CMakeLists.txt index a9584442a9..d7ec5b01fb 100644 --- a/cocos/audio/CMakeLists.txt +++ b/cocos/audio/CMakeLists.txt @@ -24,7 +24,7 @@ set(COCOS_AUDIO_SRC # stupid, clang always compie .mm as objc/cpp mix obj if(ANDROID) set(COCOS_AUDIO_SRC ${COCOS_AUDIO_SRC} - audio/src/AudioEngineImpl.cpp + audio/src/alink.cpp ) else() set(COCOS_AUDIO_SRC ${COCOS_AUDIO_SRC} diff --git a/cocos/audio/include/alconfig.h b/cocos/audio/include/alconfig.h index 2224bbfd1a..4fdb9b5b5b 100644 --- a/cocos/audio/include/alconfig.h +++ b/cocos/audio/include/alconfig.h @@ -35,13 +35,10 @@ #define MAX_AUDIOINSTANCES 24 #define CC_USE_ALSOFT 0 #else -#ifdef OPENAL_PLAIN_INCLUDES -#include -#include -#else -#include -#include -#endif +#define AL_ALEXT_PROTOTYPES 1 +#include "AL/al.h" +#include "AL/alc.h" +#include "AL/alext.h" #define MAX_AUDIOINSTANCES 32 #define CC_USE_ALSOFT 1 #endif diff --git a/cocos/audio/src/AudioEngineImpl.cpp b/cocos/audio/src/AudioEngineImpl.cpp deleted file mode 100644 index c762a7128a..0000000000 --- a/cocos/audio/src/AudioEngineImpl.cpp +++ /dev/null @@ -1,2 +0,0 @@ -/* Android clang link workaround for unrefenreced "gnu_objc_personality_v0" */ -#include "AudioEngineImpl.mm" diff --git a/cocos/audio/src/AudioEngineImpl.mm b/cocos/audio/src/AudioEngineImpl.mm index 6b1262d21c..0917bf10f4 100644 --- a/cocos/audio/src/AudioEngineImpl.mm +++ b/cocos/audio/src/AudioEngineImpl.mm @@ -50,6 +50,27 @@ static ALCdevice* s_ALDevice = nullptr; static ALCcontext* s_ALContext = nullptr; static AudioEngineImpl* s_instance = nullptr; +static void ccALPauseDevice() { + ALOGD("%s", "===> ccALPauseDevice"); +#if CC_USE_ALSOFT + alcDevicePauseSOFT(s_ALDevice); +#else + if(alcGetCurrentContext()) + alcMakeContextCurrent(nullptr); +#endif +} + +static void ccALResumeDevice() { + ALOGD("%s", "===> ccALResumeDevice"); +#if CC_USE_ALSOFT + alcDeviceResumeSOFT(s_ALDevice); +#else + if(alcGetCurrentContext()) + alcMakeContextCurrent(nullptr); + alcMakeContextCurrent(s_ALContext); +#endif +} + #if defined(__APPLE__) typedef ALvoid (*alSourceNotificationProc)(ALuint sid, ALuint notificationID, ALvoid* userData); @@ -88,14 +109,14 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio { if (kAudioSessionBeginInterruption == interruption_state) { - alcMakeContextCurrent(nullptr); + ccALPauseDevice(); } else if (kAudioSessionEndInterruption == interruption_state) { OSStatus result = AudioSessionSetActive(true); if (result) NSLog(@"Error setting audio session active! %d\n", static_cast(result)); - alcMakeContextCurrent(s_ALContext); + ccALResumeDevice(); } } #endif @@ -142,16 +163,16 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { ALOGD("AVAudioSessionInterruptionTypeBegan, application != UIApplicationStateActive, alcMakeContextCurrent(nullptr)"); - alcMakeContextCurrent(nullptr); } else { ALOGD("AVAudioSessionInterruptionTypeBegan, application == UIApplicationStateActive, pauseOnResignActive = true"); - pauseOnResignActive = true; } + + // We always pause device when interruption began + ccALPauseDevice(); } - - if (reason == AVAudioSessionInterruptionTypeEnded) + else if (reason == AVAudioSessionInterruptionTypeEnded) { isAudioSessionInterrupted = false; @@ -160,9 +181,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio ALOGD("AVAudioSessionInterruptionTypeEnded, application == UIApplicationStateActive, alcMakeContextCurrent(s_ALContext)"); NSError *error = nil; [[AVAudioSession sharedInstance] setActive:YES error:&error]; - if (alcGetCurrentContext() != nullptr) - alcMakeContextCurrent(nullptr); - alcMakeContextCurrent(s_ALContext); + ccALResumeDevice(); if (Director::getInstance()->isPaused()) { ALOGD("AVAudioSessionInterruptionTypeEnded, director was paused, try to resume it."); @@ -183,7 +202,7 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio { pauseOnResignActive = false; ALOGD("UIApplicationWillResignActiveNotification, alcMakeContextCurrent(nullptr)"); - alcMakeContextCurrent(nullptr); + ccALPauseDevice(); } } else if ([notification.name isEqualToString:UIApplicationDidBecomeActiveNotification]) @@ -201,14 +220,11 @@ void AudioEngineInterruptionListenerCallback(void* user_data, UInt32 interruptio } [[AVAudioSession sharedInstance] setActive:YES error:&error]; - if (alcGetCurrentContext() != nullptr) - alcMakeContextCurrent(nullptr); - alcMakeContextCurrent(s_ALContext); + ccALResumeDevice(); } else if (isAudioSessionInterrupted) { - ALOGD("Audio session is still interrupted, pause director!"); - // Director::getInstance()->pause(); + ALOGD("Audio session is still interrupted!"); } } } diff --git a/cocos/audio/src/alink.cpp b/cocos/audio/src/alink.cpp new file mode 100644 index 0000000000..f78abcd434 --- /dev/null +++ b/cocos/audio/src/alink.cpp @@ -0,0 +1,2 @@ +/* alink.cpp: only a workaround for solving clang link issue, said: unrefenreced "gnu_objc_personality_v0" */ +#include "AudioEngineImpl.mm"