Added a hint SDL_HINT_AUDIO_CATEGORY to control the audio category,
authorSam Lantinga <slouken@libsdl.org>
Fri, 15 Sep 2017 17:27:32 -0700
changeset 115018ab64b5850ae
parent 11500 feab2c8e1bf5
child 11502 f778543ed8dd
Added a hint SDL_HINT_AUDIO_CATEGORY to control the audio category,
determining whether the phone mute switch affects the audio
WhatsNew.txt
include/SDL_hints.h
src/audio/coreaudio/SDL_coreaudio.m
     1.1 --- a/WhatsNew.txt	Thu Sep 14 21:45:14 2017 +0200
     1.2 +++ b/WhatsNew.txt	Fri Sep 15 17:27:32 2017 -0700
     1.3 @@ -57,6 +57,8 @@
     1.4  Linux:
     1.5  * Added an experimental KMS/DRM video driver for embedded development
     1.6  
     1.7 +iOS:
     1.8 +* Added a hint SDL_HINT_AUDIO_CATEGORY to control the audio category, determining whether the phone mute switch affects the audio
     1.9  
    1.10  ---------------------------------------------------------------------------
    1.11  2.0.5:
     2.1 --- a/include/SDL_hints.h	Thu Sep 14 21:45:14 2017 +0200
     2.2 +++ b/include/SDL_hints.h	Fri Sep 15 17:27:32 2017 -0700
     2.3 @@ -855,6 +855,19 @@
     2.4  #define SDL_HINT_AUDIO_RESAMPLING_MODE   "SDL_AUDIO_RESAMPLING_MODE"
     2.5  
     2.6  /**
     2.7 + *  \brief  A variable controlling the audio category on iOS and Mac OS X
     2.8 + *
     2.9 + *  This variable can be set to the following values:
    2.10 + *
    2.11 + *    "ambient"     - Use the AVAudioSessionCategoryAmbient audio category, will be muted by the phone mute switch (default)
    2.12 + *    "playback"    - Use the AVAudioSessionCategoryPlayback category
    2.13 + *
    2.14 + *  For more information, see Apple's documentation:
    2.15 + *  https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategoriesandModes/AudioSessionCategoriesandModes.html
    2.16 + */
    2.17 +#define SDL_HINT_AUDIO_CATEGORY   "SDL_AUDIO_CATEGORY"
    2.18 +
    2.19 +/**
    2.20   *  \brief  An enumeration of hint priorities
    2.21   */
    2.22  typedef enum
     3.1 --- a/src/audio/coreaudio/SDL_coreaudio.m	Thu Sep 14 21:45:14 2017 +0200
     3.2 +++ b/src/audio/coreaudio/SDL_coreaudio.m	Fri Sep 15 17:27:32 2017 -0700
     3.3 @@ -25,6 +25,7 @@
     3.4  /* !!! FIXME: clean out some of the macro salsa in here. */
     3.5  
     3.6  #include "SDL_audio.h"
     3.7 +#include "SDL_hints.h"
     3.8  #include "../SDL_audio_c.h"
     3.9  #include "../SDL_sysaudio.h"
    3.10  #include "SDL_coreaudio.h"
    3.11 @@ -325,7 +326,8 @@
    3.12      @autoreleasepool {
    3.13          AVAudioSession *session = [AVAudioSession sharedInstance];
    3.14          NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    3.15 -        NSString *category;
    3.16 +		/* Set category to ambient by default so that other music continues playing. */
    3.17 +        NSString *category = AVAudioSessionCategoryAmbient;
    3.18          NSError *err = nil;
    3.19  
    3.20          if (open_playback_devices && open_capture_devices) {
    3.21 @@ -333,10 +335,17 @@
    3.22          } else if (open_capture_devices) {
    3.23              category = AVAudioSessionCategoryRecord;
    3.24          } else {
    3.25 -            /* Set category to ambient so that other music continues playing.
    3.26 -             You can change this at runtime in your own code if you need different
    3.27 -             behavior. If this is common, we can add an SDL hint for this. */
    3.28 -            category = AVAudioSessionCategoryAmbient;
    3.29 +            const char *hint = SDL_GetHint(SDL_HINT_AUDIO_CATEGORY);
    3.30 +            if (hint) {
    3.31 +                if (SDL_strcasecmp(hint, "AVAudioSessionCategoryAmbient") == 0) {
    3.32 +                    category = AVAudioSessionCategoryAmbient;
    3.33 +                } else if (SDL_strcasecmp(hint, "AVAudioSessionCategorySoloAmbient") == 0) {
    3.34 +                    category = AVAudioSessionCategorySoloAmbient;
    3.35 +                } else if (SDL_strcasecmp(hint, "AVAudioSessionCategoryPlayback") == 0 ||
    3.36 +                           SDL_strcasecmp(hint, "playback") == 0) {
    3.37 +                    category = AVAudioSessionCategoryPlayback;
    3.38 +                }
    3.39 +            }
    3.40          }
    3.41  
    3.42          if (![session setCategory:category error:&err]) {