自从以来,你管理应用程序的音频会话的方式发生了一些重大变化,并值得首先简要提及。在之前,您将利用
AVAudioSession和
AudioSessionServices类,分别包括委托和属性监听。从开始,请使用
AVAudioSession类并融入通知。
以下内容适用于及以上版本。
要判断应用程序沙盒外是否播放其他音频,请使用 -
// query if other audio is playing
BOOL isPlayingWithOthers = [[AVAudioSession sharedInstance] isOtherAudioPlaying]
// test it with...
(isPlayingWithOthers) ? NSLog(@"other audio is playing") : NSLog(@"no other audio is playing")
关于中断处理,您需要观察AVAudioSessionInterruptionNotification
和AVAudioSessionRouteChangeNotification
。因此,在管理音频会话的类中,您可以添加以下内容-应在应用程序生命周期的开始调用一次,并不要忘记在同一类的dealloc方法中删除观察者。
// ensure we already have a singleton object
[AVAudioSession sharedInstance]
// register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interruption:)
name:AVAudioSessionInterruptionNotification
object:nil]
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(routeChange:)
name:AVAudioSessionRouteChangeNotification
object:nil]
最后,添加以下选择器interruption:
和routeChange:
- 这些将接收到一个名为userInfo的类型为NSDictionary
的属性NSNotification
对象,您可以读取它以辅助应用程序的任何条件。
- (void)interruption:(NSNotification*)notification {
NSDictionary *interuptionDict = notification.userInfo;
NSInteger interuptionType = [[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey] integerValue];
switch (interuptionType) {
case AVAudioSessionInterruptionTypeBegan:
NSLog(@"Audio Session Interruption case started.");
break;
case AVAudioSessionInterruptionTypeEnded:
NSLog(@"Audio Session Interruption case ended.");
break;
default:
NSLog(@"Audio Session Interruption Notification case default.");
break;
} }
同样地...
- (void)routeChange:(NSNotification*)notification {
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonUnknown:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonUnknown");
break;
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonNewDeviceAvailable");
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonCategoryChange");
break;
case AVAudioSessionRouteChangeReasonOverride:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonOverride");
break;
case AVAudioSessionRouteChangeReasonWakeFromSleep:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonWakeFromSleep");
break;
case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
NSLog(@"routeChangeReason : AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory");
break;
default:
break;
} }
只要在您的应用程序生命周期的根视图控制器的
viewDidLoad
中检查应用程序音频会话的状态,就无需轮询任何内容。从那时起对您的应用程序音频会话的任何更改都将通过这两个主要通知进行通知。将
NSLog
语句替换为基于开关中包含的情况执行的代码。
您可以在
AVAudioSession
类参考文档中找到有关
AVAudioSessionInterruptionTypeKey
和
AVAudioSessionRouteChangeReasonKey
的更多信息。
很抱歉我的答案有点长,但我认为iOS中的音频会话管理相当麻烦,在撰写本文时,苹果的音频会话编程指南不包括使用通知进行中断处理的代码示例。
[[AVAudioSession sharedInstance] setActive:YES error:&theError]
。 - IMFletcher