我已经使用此方法How do I get my AVPlayer to play while app is in background?让AVPlayer在后台播放,以便用户可以在浏览Safari时进行监听等。现在只有问题是AVPlayer会在有来电时继续播放并停留在电话中播放。有没有办法捕获来电和结束通话事件,以便AVPlayer可以手动停止和启动?
答案 0 :(得分:1)
通过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
答案 1 :(得分:1)
您可以使用CallKit立即获取电话事件。 (iOS 10.0以上) 这对我有好处。
#import <CallKit/CallKit.h>
@interface ViewController ()<CXCallObserverDelegate> {
CXCallObserver *_center;
}
-(void)viewDidLoad
_center = [[CXCallObserver alloc] init];
dispatch_queue_t queue = dispatch_queue_create("THIS_IS_A_CALL",NULL);
[_center setDelegate:self queue:queue];
委托
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
NSString *tag = @"callObserver";
NSString *content = @"";
if (call.isOutgoing) {
content = @"call.isOutgoing";
}
if (call.hasEnded) {
content = @"call.hasEnded";
}
if (call.hasConnected) {
content = @"call.hasConnected";
}
if (call.isOnHold) {
content = @"call.isOnHold";
}
NSLog(@"%@ - %@", tag, content);
}