AVAudioSession之currentRoute

原型

@property(readonly) AVAudioSessionRouteDescription *currentRoute;


## Discussion

This object describes the input and output ports associated with the current audio route.

currentRoute用于控制当前app的输入/输出设备,比如麦克风、扬声器或耳机。
这个属性完全由系统控制,所以有时候会出现一些莫名其妙的问题。比如在播放音乐的时候,明明进度在走,但就是没声音。

由于声音是所有app公用的设备,所以apple设计了一个比较复杂的打断逻辑。当声音冲突的事件出现时

其他app,会收到AVAudioSessionInterruptionNotification。
当打断恢复时,currentRoute系统会帮忙设置为打断前的值。每个app的currentRoute都不同,所以系统应该有记录每个app在使用的route。

在app内部,没有打断消息,但是会收到AVAudioSessionRouteChangeNotification(没错,耳机插拔也是这个通知),reason为AVAudioSessionRouteChangeReasonCategoryChange。
当打断恢复时,iOS也会把category还原到上一个状态(这是符合预期的)。在还原的时候还会设置currentRoute

  1. 如果修改category时,设备处于活跃状态,那么currentRoute不还原这个设备
  2. 如果处于不活跃状态,那么currentRoute会还原到之前的状态

简单来说,如果你在播放的时候录音,假设录音是AVAudioSessionCategoryRecord,output会从Speaker变为nil,录音停止的时候output还是nil;而如果你在录音时没有播放,虽然output也会从Speaker变为nil,但是停止的时候会还原为Speaker。

你可能感兴趣的:(AVAudioSession之currentRoute)