我需要一個簡單的示例程序來通過NSNotificationCenter在Swift中發送和接收消息? 即時通訊使用核心音頻,我需要通知我的應用程序,如果在播放音頻時頭部手機被移除。我不知道是否應該將觀察者添加到應用程序委託或我的視圖中,因爲我必須繼續在後臺播放音頻。通過NSNotificationCenter在swift中發送和接收消息?
這是我用來控制路線變化以瞭解耳機是否被移除的功能。
-(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
}
}
用於迅速2.0和3.0迅速檢查http://stackoverflow.com/questions/27315228/ send-and-receive-messages-through-nsnotificationcenter-in-swift?rq = 1 – Sahil 2017-02-01 13:07:03