如何在AppRTC iOS示例中将音频重定向到扬声器?

时间:2014-07-06 11:58:23

标签: ios audio avfoundation webrtc apprtcdemo

我尝试将音频重定向到AppRTC iOS example中的扬声器。

我试过了:

AVAudioSession* session = [AVAudioSession sharedInstance];

//error handling
BOOL success;
NSError* error;

//set the audioSession category. 
//Needs to be Record or PlayAndRecord to use audioRouteOverride:  

success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                         error:&error];

if (!success)  NSLog(@"AVAudioSession error setting category:%@",error);

//set the audioSession override
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
                                      error:&error];
if (!success)  NSLog(@"AVAudioSession error overrideOutputAudioPort:%@",error);

//activate the audio session
success = [session setActive:YES error:&error];
if (!success) NSLog(@"AVAudioSession error activating: %@",error);
else NSLog(@"audioSession active");

没有错误,但它不起作用。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:13)

我通过解决方案解决了这个问题。 只听AVAudioSessionRouteChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSessionRouteChange:) name:AVAudioSessionRouteChangeNotification object:nil];

使用didSessionRouteChange选择器如下:

- (void)didSessionRouteChange:(NSNotification *)notification
{
  NSDictionary *interuptionDict = notification.userInfo;
  NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];

  switch (routeChangeReason) {
      case AVAudioSessionRouteChangeReasonCategoryChange: {
          // Set speaker as default route
          NSError* error;
          [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
      }
      break;

    default:
      break;
  }
}

答案 1 :(得分:2)

对于来到这里的任何人,在Swift中寻找一种解决方案,该解决方案还应考虑到(BT-)耳机带来的变化。下面的示例(Swift 5)做到了。
@Teivaz部分采用

@objc func handleRouteChange(notification: Notification) {
    guard let info = notification.userInfo,
        let value = info[AVAudioSessionRouteChangeReasonKey] as? UInt,
        let reason = AVAudioSession.RouteChangeReason(rawValue: value) else { return }

    switch reason {
    case .categoryChange:
        try? AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
    case .oldDeviceUnavailable:
        try? AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
    default:
        l.debug("other")
    }
}

答案 2 :(得分:1)

似乎还是一个问题,phuongle的回答对我有用。 Swift 4版本:

NotificationCenter.default.addObserver(forName: .AVAudioSessionRouteChange, object: nil, queue: nil, using: routeChange)    

private func routeChange(_ n: Notification) {
    guard let info = n.userInfo,
        let value = info[AVAudioSessionRouteChangeReasonKey] as? UInt,
        let reason = AVAudioSessionRouteChangeReason(rawValue: value) else { return }
    switch reason {
    case .categoryChange: try? AVAudioSession.sharedInstance().overrideOutputAudioPort(.speaker)
    default: break
    }
}

答案 3 :(得分:0)

我最终找到了解决方案。

原因是您需要将AVAudioSession类别设置为AVAudioSessionCategoryPlayback。但由于某些原因,在建立webRTC呼叫后,它被设置回AVAudioSessionCategoryPlayAndRecord。 最后,我决定为AVAudioSessionRouteChangeNotification添加观察者,并在每次检测到不需要的类别更改时切换到AVAudioSessionCategoryPlayback。 一些黑客解决方案,但最终工作。您可以查看here

答案 4 :(得分:0)

phuongle's answer是正确的。虽然当您启用覆盖时,即使用户插入耳机,这实际上也会覆盖音频输出。当用户使用耳机时,通过扬声器播放音频没有多大意义。为此,请使用以下代码:

$sce