[Android] Fixes #2480, music does not pause when process backgrounded This modifies SDL_PauseAudio behavior to pause all audio devices instead of just the default one (required on Android, at least for testmultiaudio on my Nexus 4 which reported 2 audio devices). It also changes SDL_PauseAudioDevice to retain the device lock from pause until resume in order to save battery in mobile devices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 7559d6c..326bef6 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -1348,17 +1348,26 @@ void
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
{
SDL_AudioDevice *device = get_audio_device(devid);
- if (device) {
- current_audio.impl.LockDevice(device);
+ if (device && device->paused != pause_on) {
+ if (pause_on) {
+ current_audio.impl.LockDevice(device);
+ }
device->paused = pause_on;
- current_audio.impl.UnlockDevice(device);
+ if (!pause_on) {
+ current_audio.impl.UnlockDevice(device);
+ }
}
}
void
SDL_PauseAudio(int pause_on)
{
- SDL_PauseAudioDevice(1, pause_on);
+ int id;
+ for (id = 0; id < SDL_arraysize(open_devices); id++) {
+ if (open_devices[id] != NULL) {
+ SDL_PauseAudioDevice(id+1, pause_on);
+ }
+ }
}
diff --git a/src/video/android/SDL_androidevents.c b/src/video/android/SDL_androidevents.c
index 907d730..51d3c4b 100644
--- a/src/video/android/SDL_androidevents.c
+++ b/src/video/android/SDL_androidevents.c
@@ -74,13 +74,14 @@ Android_PumpEvents(_THIS)
if (isPaused && !isPausing) {
/* Make sure this is the last thing we do before pausing */
android_egl_context_backup();
+ SDL_PauseAudio(1);
if(SDL_SemWait(Android_ResumeSem) == 0) {
#else
if (isPaused) {
if(SDL_SemTryWait(Android_ResumeSem) == 0) {
#endif
isPaused = 0;
-
+ SDL_PauseAudio(0);
/* Restore the GL Context from here, as this operation is thread dependent */
if (!SDL_HasEvent(SDL_QUIT)) {
android_egl_context_restore();
@@ -103,6 +104,7 @@ Android_PumpEvents(_THIS)
#else
if(SDL_SemTryWait(Android_PauseSem) == 0) {
android_egl_context_backup();
+ SDL_PauseAudio(1);
isPaused = 1;
}
#endif
diff --git a/test/testmultiaudio.c b/test/testmultiaudio.c
index 985c05a..cc3cdcb 100644
--- a/test/testmultiaudio.c
+++ b/test/testmultiaudio.c
@@ -50,6 +50,12 @@ test_multi_audio(int devcount)
callback_data cbd[64];
int keep_going = 1;
int i;
+
+#ifdef __ANDROID__
+ SDL_Event event;
+
+ SDL_CreateWindow("testmultiaudio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
+#endif
if (devcount > 64) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
@@ -71,8 +77,12 @@ test_multi_audio(int devcount)
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
} else {
SDL_PauseAudioDevice(cbd[0].dev, 0);
- while (!cbd[0].done)
+ while (!cbd[0].done) {
+#ifdef __ANDROID__
+ while (SDL_PollEvent(&event)){}
+#endif
SDL_Delay(100);
+ }
SDL_PauseAudioDevice(cbd[0].dev, 1);
SDL_Log("done.\n");
SDL_CloseAudioDevice(cbd[0].dev);
@@ -104,6 +114,9 @@ test_multi_audio(int devcount)
keep_going = 1;
}
}
+#ifdef __ANDROID__
+ while (SDL_PollEvent(&event)){}
+#endif
SDL_Delay(100);
}