2

我正在尝试使用 Novocaine 播放一些高性能的音频,但我找到的示例代码仅在耳机中播放音频,但我希望它在扬声器上播放......这可能吗?

谢谢

4

2 回答 2

5

我已经实现了这样的路线更改,它似乎有效。只需用sessionPropertyListener下面的代码替换并添加updateAudioRoute.

void sessionPropertyListener(void *                  inClientData,
                             AudioSessionPropertyID  inID,
                             UInt32                  inDataSize,
                             const void *            inData){


    if (inID == kAudioSessionProperty_AudioRouteChange)
    {
        Novocaine *sm = (Novocaine *)inClientData;
        [sm checkSessionProperties];
        [sm updateAudioRoute];
    }
}

//Quick and dirty way to override the audioRoute whenever the audioRoute is changed.
- (void)updateAudioRoute {
  CFStringRef newRoute;
  UInt32 size = sizeof(CFStringRef);
  CheckError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), 
             "couldn't get new audio route");
  if (newRoute)
  {
    CFShow(newRoute);
    if (CFStringCompare(newRoute, CFSTR("ReceiverAndMicrophone"), (UInt32)NULL)== kCFCompareEqualTo)
    {
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    }
    else if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), (UInt32)NULL) == kCFCompareEqualTo)
    {
      UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
      AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    }
  }
}
于 2012-12-28T01:32:43.457 回答
3

有几乎同样的问题。快速而肮脏的修复,添加:

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

到 iOS 部分的末尾 - (void)setupAudio in your Novocaine.m
请注意,即使插入了耳机,这也会通过扬声器播放音频!

在我看来,路由更改尚未实现,因为 -(void)selectAudioDevice 为空。

于 2012-12-10T05:07:38.080 回答