- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//播放时候,来电话的处理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[self.window makeKeyAndVisible];
return YES;
}
//播放时候,来电话的处理
- (void)onAudioSessionEvent:(NSNotification *)notic{
NSDictionary *interruptionDictionary = [notic userInfo];
AVAudioSessionInterruptionType type =
[interruptionDictionary [AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
NSLog(@"Interruption started");
/*add暂停播放code*/
} else if (type == AVAudioSessionInterruptionTypeEnded){
NSLog(@"Interruption ended");
UIBackgroundTaskIdentifier bgTask = 0;
if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {
NSLog(@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx后台播放");
/*add继续播放code*/
UIApplication*app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
if(bgTask!= UIBackgroundTaskInvalid) {
[app endBackgroundTask:bgTask];
}
bgTask = newTask;
}else {
// NSLog(@"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx前台播放");
/*add继续播放code*/
}
} else {
NSLog(@"Something else happened");
}
}
//播放时候,来电话的处理
最新推荐文章于 2019-08-09 01:57:57 发布