[Android] Fixes #2480, music does not pause when process backgrounded
authorGabriel Jacobo <gabomdq@gmail.com>
Wed, 17 Sep 2014 11:41:12 -0300
changeset 9145aa99e029b12e
parent 9144 c197a316685a
child 9146 dbef1f283c3f
[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.
src/audio/SDL_audio.c
src/video/android/SDL_androidevents.c
test/testmultiaudio.c
     1.1 --- a/src/audio/SDL_audio.c	Wed Sep 17 08:59:43 2014 -0300
     1.2 +++ b/src/audio/SDL_audio.c	Wed Sep 17 11:41:12 2014 -0300
     1.3 @@ -1348,17 +1348,26 @@
     1.4  SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
     1.5  {
     1.6      SDL_AudioDevice *device = get_audio_device(devid);
     1.7 -    if (device) {
     1.8 -        current_audio.impl.LockDevice(device);
     1.9 +    if (device && device->paused != pause_on) {
    1.10 +        if (pause_on) {
    1.11 +            current_audio.impl.LockDevice(device);
    1.12 +        }
    1.13          device->paused = pause_on;
    1.14 -        current_audio.impl.UnlockDevice(device);
    1.15 +        if (!pause_on) {
    1.16 +            current_audio.impl.UnlockDevice(device);
    1.17 +        }
    1.18      }
    1.19  }
    1.20  
    1.21  void
    1.22  SDL_PauseAudio(int pause_on)
    1.23  {
    1.24 -    SDL_PauseAudioDevice(1, pause_on);
    1.25 +    int id;
    1.26 +    for (id = 0; id < SDL_arraysize(open_devices); id++) {
    1.27 +        if (open_devices[id] != NULL) {
    1.28 +            SDL_PauseAudioDevice(id+1, pause_on);
    1.29 +        }
    1.30 +    }
    1.31  }
    1.32  
    1.33  
     2.1 --- a/src/video/android/SDL_androidevents.c	Wed Sep 17 08:59:43 2014 -0300
     2.2 +++ b/src/video/android/SDL_androidevents.c	Wed Sep 17 11:41:12 2014 -0300
     2.3 @@ -74,13 +74,14 @@
     2.4      if (isPaused && !isPausing) {
     2.5          /* Make sure this is the last thing we do before pausing */
     2.6          android_egl_context_backup();
     2.7 +        SDL_PauseAudio(1);
     2.8          if(SDL_SemWait(Android_ResumeSem) == 0) {
     2.9  #else
    2.10      if (isPaused) {
    2.11          if(SDL_SemTryWait(Android_ResumeSem) == 0) {
    2.12  #endif
    2.13              isPaused = 0;
    2.14 -            
    2.15 +            SDL_PauseAudio(0);
    2.16              /* Restore the GL Context from here, as this operation is thread dependent */
    2.17              if (!SDL_HasEvent(SDL_QUIT)) {
    2.18                  android_egl_context_restore();
    2.19 @@ -103,6 +104,7 @@
    2.20  #else
    2.21          if(SDL_SemTryWait(Android_PauseSem) == 0) {
    2.22              android_egl_context_backup();
    2.23 +            SDL_PauseAudio(1);
    2.24              isPaused = 1;
    2.25          }
    2.26  #endif
     3.1 --- a/test/testmultiaudio.c	Wed Sep 17 08:59:43 2014 -0300
     3.2 +++ b/test/testmultiaudio.c	Wed Sep 17 11:41:12 2014 -0300
     3.3 @@ -50,6 +50,12 @@
     3.4      callback_data cbd[64];
     3.5      int keep_going = 1;
     3.6      int i;
     3.7 +    
     3.8 +#ifdef __ANDROID__  
     3.9 +    SDL_Event event;
    3.10 +  
    3.11 +    SDL_CreateWindow("testmultiaudio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
    3.12 +#endif
    3.13  
    3.14      if (devcount > 64) {
    3.15          SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
    3.16 @@ -71,8 +77,12 @@
    3.17              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
    3.18          } else {
    3.19              SDL_PauseAudioDevice(cbd[0].dev, 0);
    3.20 -            while (!cbd[0].done)
    3.21 +            while (!cbd[0].done) {
    3.22 +#ifdef __ANDROID__                
    3.23 +                while (SDL_PollEvent(&event)){}
    3.24 +#endif                
    3.25                  SDL_Delay(100);
    3.26 +            }
    3.27              SDL_PauseAudioDevice(cbd[0].dev, 1);
    3.28              SDL_Log("done.\n");
    3.29              SDL_CloseAudioDevice(cbd[0].dev);
    3.30 @@ -104,6 +114,9 @@
    3.31                  keep_going = 1;
    3.32              }
    3.33          }
    3.34 +#ifdef __ANDROID__        
    3.35 +        while (SDL_PollEvent(&event)){}
    3.36 +#endif        
    3.37          SDL_Delay(100);
    3.38      }
    3.39