在我目前正在构建的应用程序中,我需要根据[AVAudioSession sharedInstance]的当前类别处理一些事情。这个类别在运行时会发生变化,我试图通过在NotificationCenter中观察AVAudioSessionRouteChangeNotification来跟踪这些变化。
但是,无论我尝试了什么,通知都不会被触发(或者至少我的选择器方法没有收到任何消息)。
一些测试样例代码;
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionRouteDidChange:) name:AVAudioSessionRouteChangeNotification object:nil];
return YES;
}
- (void)sessionRouteDidChange:(NSNotification *)notification
{
NSLog(@"SESSION ROUTE CHANGED");
}ViewController.m
- (void)changeAudioCategory:(AVAudioSessionCategory)category{
[[AVAudioSession sharedInstance] setActive:NO error:nil];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:category error:&setCategoryError];
if (!success) {
NSLog(@"ERROR");
}
[audioSession setActive:YES error:nil];
/* Logging the current category */
NSLog(@"AVAUDIOSESSION CATEGORY %@", audioSession.category);
}我期望在AppDelegate中设置的观察者在此方法中更改类别时向sessionRouteDidChange发送通知,但不会发生任何事情。
我尝试过的;
对我错过了什么有什么想法吗?
发布于 2017-11-30 15:05:54
更改类别不是路由更改,因此它不会发布通知。您想要的是KVO观察 category属性。
当您更改音频路由时,例如当您插入耳机时,就会发生路由更改.
https://stackoverflow.com/questions/47571688
复制相似问题