We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5239 - Play audio on Android while backgrounded?
Summary: Play audio on Android while backgrounded?
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: audio (show other bugs)
Version: 2.0.12
Hardware: x86 Windows 10
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-07-19 19:34 UTC by superfury
Modified: 2020-09-25 13:57 UTC (History)
1 user (show)

See Also:


Attachments
Changes to SDL Android using the new hint. (5.76 KB, patch)
2020-09-24 18:09 UTC, superfury
Details | Diff
The new hint and implementation on the SDL 2.0.12 version. (5.76 KB, patch)
2020-09-24 20:41 UTC, superfury
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description superfury 2020-07-19 19:34:02 UTC
In my application, I have various settings for allowing audio and sound recording to continue while the app is backgrounded (just like on Windows, this should apply to Android builds as well).

But when I set it to play audio and stop recording audio (or play audio and keep recording audio as well), the audio output stops as soon as I minimize the app.

When I reopen the app again, the audio resumes, although there's a stutter because the audio rendering output is out-of-sync with what the app's rendering output provides (rendering from the main thread into a buffer, and from the audio thread(normal SDL audio callback that does this, using the normal handler registered using SDL_OpenAudio) reading said buffer to the actual SDL audio handler(which is the usual function that renders from said FIFO buffer in the main thread to the SDL renderer).

Anyone knows how to get it to play audio while the app is backgrounded (e.g. after the SDL_APP_DIDENTERBACKGROUND event?
Comment 1 Sylvain 2020-07-21 13:53:25 UTC
did you try to use SDL_HINT_ANDROID_BLOCK_ON_PAUSE not to block on pause ?
Comment 2 superfury 2020-07-21 19:59:12 UTC
SDL_HINT_ANDROID_BLOCK_ON_PAUSE is already set to 0, evidenced by the audio player not pausing when the app is minimized on Android.

Audio rendering, however, still seems muted.
Comment 3 Sylvain 2020-07-21 20:38:07 UTC
ok, sorry!
in:
src/video/android/SDL_androidevents.c

it calls 
 ANDROIDAUDIO_PauseDevices();
 openslES_PauseDevices();
when device is paused. only the event loop remain running.

then it resumes:
ANDROIDAUDIO_ResumeDevices();
 openslES_ResumeDevices()
Comment 4 superfury 2020-09-24 15:53:15 UTC
@Sylvain I tried your approach, disabling both of those lines you've mentioned in comment #3.

It seems to work.

I'm thinking about something. Is this safe to use this way on all Android devices and/or apps?

Or should it have a hint added to allow apps to enable this functionality (keep audio running in background)?

I've implemented it like this:

At the top of function Android_PumpEvents_NonBlocking(_THIS), below "backup_context = 0;":
    static int backup_audio = 0;

Then before "backup_context = 0" within the isPaused check(after the context backup):
            if (SDL_GetHintBoolean(SDL_HINT_ANDROID_BLOCK_ON_PAUSE_PAUSEAUDIO, SDL_TRUE)) //Stop audio when paused?
            {
                ANDROIDAUDIO_PauseDevices();
                openslES_PauseDevices();
                backup_audio = 1; //Audio is paused!
            }

Finally, after SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0); :
            if (backup_audio) //Audio is backed up?
            {
                ANDROIDAUDIO_ResumeDevices();
                openslES_ResumeDevices();
                backup_audio = 0; //Audio is resumed!
            }

And in SDL_hints.h:
/**
  * \brief A variable to control whether SDL will pause audio when not blocking the event loop itself when the app is paused.
  *
  * 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"

Then, setting the hint to SDL_FALSE will cause audio to not be paused by SDL, allowing playing audio in the background?
Comment 5 superfury 2020-09-24 18:09:20 UTC
Created attachment 4469 [details]
Changes to SDL Android using the new hint.
Comment 6 superfury 2020-09-24 18:09:47 UTC
For some reason, SDL on Android crashes with that patch?
Comment 7 superfury 2020-09-24 19:53:15 UTC
I was just thinking... Perhaps the issue is somewhere upstream from the SDL 2.0.12 tag until the current version on the default branch?
Comment 8 superfury 2020-09-24 20:06:56 UTC
Just confirmed: it's crashing on the current hg version.
Comment 9 superfury 2020-09-24 20:41:59 UTC
Created attachment 4470 [details]
The new hint and implementation on the SDL 2.0.12 version.

I've remade the patch for the SDL 2.0.12 commit.

This patch seems to run without issues, not crashing, both with the functionality enabled and disabled (and SDL_ANDROID_BLOCK_ON_PAUSE set to disabled of course).

So that 100% confirms that the current hg version of SDL has some issue somewhere with Android devices? It's crashing immediately when starting the app.
Comment 10 superfury 2020-09-25 06:21:27 UTC
Can anyone put one of the two bugfixes into the hg tree if it's ok by QA standards?
Comment 11 superfury 2020-09-25 06:59:22 UTC
About the crash, perhaps commit 13946 is the cause? I see it changing various video initialization variables, while the definition of said variables stays unchanged?
Comment 12 superfury 2020-09-25 07:08:54 UTC
Just looked at the current source code. The commit 13946 is indeed the cause in later commits.

It's changed back in current commits?

Otherwise, I see no Android changes (besides logging).

The crash seems to happen somewhere during SDL_Init.
Comment 13 Sylvain 2020-09-25 08:18:57 UTC
I changed a little bit the patch so there is less diff: 
https://hg.libsdl.org/SDL/rev/064eaa6b911d


I see no crash with latest head. Maybe clean + recompile. Don't forget to update the java side, if needed.
(changeset 13946 is https://hg.libsdl.org/SDL/rev/5acc27d3d654 and doesn't look strange)
Comment 14 superfury 2020-09-25 11:36:26 UTC
Just rebuilt with updating the SDL Java files. That seems to have fixed the crash! :D
Comment 15 superfury 2020-09-25 11:38:29 UTC
The fix is now implemented in hg and fixed. Thanks for the help!
Comment 16 Sylvain 2020-09-25 13:57:58 UTC
ok great thanks !