Pause/resume sounds when game is paused on Android.

We should handle both pause and focus events, but they may occur in different order or in some cases we may receive only lost focus event.
So we need to check the current state to make sure that pause/resume sounds is executed only once.
This commit is contained in:
Deve 2017-01-05 15:52:24 +01:00
parent c7f9de3080
commit 5f4bef044b

View File

@ -17,6 +17,8 @@
#include "guiengine/event_handler.hpp"
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "guiengine/abstract_state_manager.hpp"
@ -39,6 +41,10 @@
#include <iostream>
#ifdef ANDROID
#include <android_native_app_glue.h>
#endif
using GUIEngine::EventHandler;
using GUIEngine::EventPropagation;
@ -148,6 +154,36 @@ bool EventHandler::OnEvent (const SEvent &event)
{
return onGUIEvent(event) == EVENT_BLOCK;
}
#ifdef ANDROID
else if (event.EventType == EET_SYSTEM_EVENT &&
event.SystemEvent.EventType == ESET_ANDROID_CMD)
{
int cmd = event.SystemEvent.AndroidCmd.Cmd;
IrrlichtDevice* device = irr_driver->getDevice();
assert(device != NULL);
if (cmd == APP_CMD_PAUSE || cmd == APP_CMD_LOST_FOCUS)
{
// Make sure that pause/unpause is executed only once
if (device->isWindowMinimized() == device->isWindowFocused())
{
music_manager->pauseMusic();
SFXManager::get()->pauseAll();
}
}
else if (cmd == APP_CMD_RESUME || cmd == APP_CMD_GAINED_FOCUS)
{
if (device->isWindowActive())
{
music_manager->resumeMusic();
SFXManager::get()->resumeAll();
}
}
return false;
}
#endif
else if (GUIEngine::getStateManager()->getGameState() != GUIEngine::GAME &&
event.EventType != EET_KEY_INPUT_EVENT && event.EventType != EET_JOYSTICK_INPUT_EVENT &&
event.EventType != EET_LOG_TEXT_EVENT)