2
我已經使用此方法How do I get my AVPlayer to play while app is in background?在後臺播放AVPlayer,以便用戶可以在瀏覽Safari瀏覽器時進行收聽等。現在只有問題是AVPlayer在有來電時繼續播放並在通話過程中保持播放。有沒有辦法捕捉來電和結束通話事件,以便AVPlayer可以停止並手動啓動?如何中斷AVPlayer手動撥入電話
我已經使用此方法How do I get my AVPlayer to play while app is in background?在後臺播放AVPlayer,以便用戶可以在瀏覽Safari瀏覽器時進行收聽等。現在只有問題是AVPlayer在有來電時繼續播放並在通話過程中保持播放。有沒有辦法捕捉來電和結束通話事件,以便AVPlayer可以停止並手動啓動?如何中斷AVPlayer手動撥入電話
通過coreTelephony
框架,我們必須找到或檢測來電。從那裏你必須發起你的本地通知來停止你的AVPlayer
。 導入後做這樣的
CTCallCenter * _callCenter = [[CTCallCenter alloc] init];
_callCenter.callEventHandler = ^(CTCall* call)
{
if ([call.callState isEqualToString:CTCallStateDisconnected])
{
NSLog(@"Call has been disconnected");
}
else if([call.callState isEqualToString:CTCallStateDialing])
{
NSLog(@"Call start");
}
else if ([call.callState isEqualToString:CTCallStateConnected])
{
NSLog(@"Call has just been connected");
}
else if([call.callState isEqualToString:CTCallStateIncoming])
{
NSLog(@"Call is incoming");
// You have to initiate/post your local notification through NSNotification center like this
[[NSNotificationCenter defaultCenter] postNotificationName:@"stopAVPlayer" object:nil];
} else
{
NSLog(@"None of the conditions");
}
};
參考這樣的:https://developer.apple.com/library/ios/navigation/#section=Frameworks&topic=CoreTelephony
的示例代碼似乎是從該鏈接丟失。應該在AppDelegate中創建CTCallCenter,然後通過NSNotificationCenter與ViewController方法進行通信?或者是否可以在View Controller中與AVPlayer實例一起創建? – TijuanaKez