diff --git a/src/modules/audio/openal/Audio.cpp b/src/modules/audio/openal/Audio.cpp index d6208c310..686c8f7d9 100644 --- a/src/modules/audio/openal/Audio.cpp +++ b/src/modules/audio/openal/Audio.cpp @@ -209,7 +209,16 @@ Audio::Audio() #ifdef LOVE_IOS love::ios::initAudioSessionInterruptionHandler(); #endif - + +#ifdef LOVE_ANDROID + bool hasPauseDeviceExt = alcIsExtensionPresent(device, "ALC_SOFT_pause_device") == ALC_TRUE; + alcDevicePauseSOFT = hasPauseDeviceExt + ? (LPALCDEVICEPAUSESOFT) alcGetProcAddress(device, "alcDevicePauseSOFT") + : nullptr; + alcDeviceResumeSOFT = hasPauseDeviceExt + ? (LPALCDEVICERESUMESOFT) alcGetProcAddress(device, "alcDeviceResumeSOFT") + : nullptr; +#endif } Audio::~Audio() @@ -319,13 +328,42 @@ std::vector Audio::pause() void Audio::pauseContext() { +#ifdef LOVE_ANDROID + if (alcDevicePauseSOFT) + alcDevicePauseSOFT(device); + else + { + // This is extremely rare case since we're using OpenAL-soft + // in Android and the ALC_SOFT_pause_device has been supported + // since 1.16 + for (auto &src: pausedSources) + src->release(); + pausedSources = pause(); + for (auto &src: pausedSources) + src->retain(); + } +#else alcMakeContextCurrent(nullptr); +#endif } void Audio::resumeContext() { +#ifdef LOVE_ANDROID + if (alcDeviceResumeSOFT) + alcDeviceResumeSOFT(device); + else + { + // Again, this is rare case + play(pausedSources); + for (auto &src: pausedSources) + src->release(); + pausedSources.resize(0); + } +#else if (context && alcGetCurrentContext() != context) alcMakeContextCurrent(context); +#endif } std::string Audio::getPlaybackDevice() diff --git a/src/modules/audio/openal/Audio.h b/src/modules/audio/openal/Audio.h index e452f9867..b49c53bf7 100644 --- a/src/modules/audio/openal/Audio.h +++ b/src/modules/audio/openal/Audio.h @@ -181,6 +181,16 @@ private: DistanceModel distanceModel; //float metersPerUnit = 1.0; + +#ifdef LOVE_ANDROID +# ifndef ALC_SOFT_pause_device + typedef void (ALC_APIENTRY*LPALCDEVICEPAUSESOFT)(ALCdevice *device); + typedef void (ALC_APIENTRY*LPALCDEVICERESUMESOFT)(ALCdevice *device); +# endif + LPALCDEVICEPAUSESOFT alcDevicePauseSOFT; + LPALCDEVICERESUMESOFT alcDeviceResumeSOFT; + std::vector pausedSources; +#endif }; // Audio #ifdef ALC_EXT_EFX diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index bde9d0bfb..9039a5de7 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -640,20 +640,9 @@ Message *Event::convertWindowEvent(const SDL_Event &e) if (auto audio = Module::getInstance(Module::M_AUDIO)) { if (e.window.event == SDL_WINDOWEVENT_MINIMIZED) - { - for (auto &src : pausedSources) - src->release(); - pausedSources = audio->pause(); - for (auto &src : pausedSources) - src->retain(); - } + audio->pauseContext(); else if (e.window.event == SDL_WINDOWEVENT_RESTORED) - { - audio->play(pausedSources); - for (auto &src : pausedSources) - src->release(); - pausedSources.resize(0); - } + audio->resumeContext(); } #endif break; diff --git a/src/modules/event/sdl/Event.h b/src/modules/event/sdl/Event.h index 31fe3c959..9a7913491 100644 --- a/src/modules/event/sdl/Event.h +++ b/src/modules/event/sdl/Event.h @@ -78,10 +78,6 @@ private: static std::map createKeyMap(); static std::map keys; -#ifdef LOVE_ANDROID - std::vector pausedSources; -#endif - }; // Event } // sdl