iOS Novocaine播放扬声器

时间:2012-12-09 06:19:47

标签: ios audio core-audio playback speaker

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

感谢

2 个答案:

答案 0 :(得分: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);
    }
  }
}

答案 1 :(得分:3)

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

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

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

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