【发布时间】:2015-02-03 14:01:11
【问题描述】:
我需要一个简单的示例程序在 Swift 中通过 NSNotificationCenter 发送和接收消息? 我正在使用核心音频,如果在播放音频时耳机被移除,我需要通知我的应用程序。我不知道是否应该在应用程序委托或视图中添加观察者,因为我必须继续在后台播放音频。
这是我用来控制路线改变以知道耳机是否被移除的功能。
-(void)handleRouteChange:(NSNotification *)notif
{
NSDictionary *dict = notif.userInfo;
AVAudioSessionRouteDescription *routeDesc = dict[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *prevPort = [routeDesc.outputs objectAtIndex:0];
if ([prevPort.portType isEqualToString:AVAudioSessionPortHeadphones]) {
//Head phone removed
}
}
【问题讨论】:
-
对于 swift 2.0 和 swift 3.0 检查stackoverflow.com/questions/27315228/…
标签: ios iphone swift nsnotificationcenter nsnotifications