Routing iPhone Audio Sound

后端 未结 3 587
醉话见心
醉话见心 2021-02-04 20:08

I have an app which does listen and play sound at the same time. By default, the sound output goes through the earphone. So I use the following code to route it through the spea

3条回答
  •  后悔当初
    2021-02-04 20:32

    This is a quick and dirty way and seems to work for me:

    void sessionPropertyListener(void *                  inClientData,
                                 AudioSessionPropertyID  inID,
                                 UInt32                  inDataSize,
                                 const void *            inData){
    
      if (inID == kAudioSessionProperty_AudioRouteChange)
      {
        CFStringRef newRoute;
        UInt32 size = sizeof(CFStringRef);
        AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute);
        if (newRoute)
        {
          CFShow(newRoute);
          if (CFStringCompare(newRoute, CFSTR("ReceiverAndMicrophone"),
                              (UInt32)NULL) == kCFCompareEqualTo)//if receiver, play through speakers
          {
            UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                                     sizeof(audioRouteOverride),
                                     &audioRouteOverride);
          }
          else if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"),
                                   (UInt32)NULL) == kCFCompareEqualTo)//headset
          {
            UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
            AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                                     sizeof(audioRouteOverride),
                                     &audioRouteOverride);
          }
        }
      }
    }
    

提交回复
热议问题