gpt4 book ai didi

ios - 通过 NSNotificationCenter 快速发送和接收消息?

转载 作者:可可西里 更新时间:2023-11-01 00:38:03 25 4
gpt4 key购买 nike

我需要一个简单的示例程序来在 Swift 中通过 NSNotificationCenter 发送和接收消息吗?我正在使用核心音频,如果在播放音频时耳机被取下,我需要通知我的应用程序。我不知道是否应该在应用程序委托(delegate)中或在我的 View 中添加观察者,因为我必须继续在后台播放音频。

这是我用来控制路线变化以了解耳机是否被移除的功能。

-(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
}
}

最佳答案

NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleRouteChange:", name: AVAudioSessionRouteChangeNotification, object: nil);
NSNotificationCenter.defaultCenter().postNotificationName(AVAudioSessionRouteChangeNotification, object: nil)

关于ios - 通过 NSNotificationCenter 快速发送和接收消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com