Skip to content

Commit

Permalink
Fixed bug 5239 - Play audio on Android while backgrounded (Thanks Sup…
Browse files Browse the repository at this point in the history
…erfury)

Add hint SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO not to pause audio when
the app goes to background.
(It requires SDL_ANDROID_BLOCK_ON_PAUSE as "Non blocking")
  • Loading branch information
1bsyl committed Sep 25, 2020
1 parent 7ef188a commit 955f318
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
12 changes: 12 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -1028,6 +1028,18 @@ extern "C" {
*/
#define SDL_HINT_ANDROID_BLOCK_ON_PAUSE "SDL_ANDROID_BLOCK_ON_PAUSE"

/**
* \brief A variable to control whether SDL will pause audio in background
* (Requires SDL_ANDROID_BLOCK_ON_PAUSE as "Non blocking")
*
* The variable can be set to the following values:
* "0" - Non paused.
* "1" - Paused. (default)
*
* The value should be set before SDL is initialized.
*/
#define SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO "SDL_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO"

/**
* \brief A variable to control whether the return key on the soft keyboard
* should hide the soft keyboard on Android and iOS.
Expand Down
12 changes: 8 additions & 4 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -175,8 +175,10 @@ Android_PumpEvents_NonBlocking(_THIS)
SDL_UnlockMutex(Android_ActivityMutex);
}

ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
if (videodata->pauseAudio) {
ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
}

backup_context = 0;
}
Expand All @@ -191,8 +193,10 @@ Android_PumpEvents_NonBlocking(_THIS)
SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);

ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();
if (videodata->pauseAudio) {
ANDROIDAUDIO_ResumeDevices();
openslES_ResumeDevices();
}

/* Restore the GL Context from here, as this operation is thread dependent */
if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
Expand Down
1 change: 1 addition & 0 deletions src/video/android/SDL_androidvideo.c
Expand Up @@ -181,6 +181,7 @@ Android_VideoInit(_THIS)

videodata->isPaused = SDL_FALSE;
videodata->isPausing = SDL_FALSE;
videodata->pauseAudio = SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO, SDL_TRUE);

mode.format = Android_ScreenFormat;
mode.w = Android_DeviceWidth;
Expand Down
1 change: 1 addition & 0 deletions src/video/android/SDL_androidvideo.h
Expand Up @@ -38,6 +38,7 @@ typedef struct SDL_VideoData
SDL_Rect textRect;
int isPaused;
int isPausing;
int pauseAudio;
} SDL_VideoData;

extern int Android_SurfaceWidth;
Expand Down

0 comments on commit 955f318

Please sign in to comment.