|
@@ -50,22 +50,35 @@
|
|
|
[self stopPlayAudioSession];
|
|
|
|
|
|
[self.timer invalidate];
|
|
|
+ _isBackgroundType = NO;
|
|
|
+
|
|
|
+ // 恢复音频播放
|
|
|
+ [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; // ok
|
|
|
+ [[AVAudioSession sharedInstance] setActive: YES error: nil];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- (void)appDidEnterBackground {
|
|
|
NSLog(@"%@ appDidEnterBackground",NSStringFromClass([self class]));
|
|
|
|
|
|
-// BOOL isBackground = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_working_background)];
|
|
|
-// if (!isBackground) {
|
|
|
-// return;
|
|
|
-// }
|
|
|
-//
|
|
|
-// _isBackgroundType = YES;
|
|
|
+ _isBackgroundType = YES;
|
|
|
+
|
|
|
+ BOOL isBackground = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_working_background)];
|
|
|
+ if (!isBackground) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
[[AVAudioSession sharedInstance] setActive: YES error: nil];
|
|
|
[self startPlayAudioSession];
|
|
|
|
|
|
|
|
|
+ //申请后台
|
|
|
+ self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
|
|
|
+ [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
|
|
|
+ self.backgrounTask = UIBackgroundTaskInvalid;
|
|
|
+ }];
|
|
|
+
|
|
|
self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(applyToSystemForMoreTime) userInfo:nil repeats:YES];
|
|
|
[self.timer setFireDate:[NSDate distantPast]];
|
|
|
}
|
|
@@ -87,14 +100,14 @@
|
|
|
/// 开始播放声音
|
|
|
- (void)startPlayAudioSession{
|
|
|
NSLog(@"%@ startPlayAudioSession",NSStringFromClass([self class]));
|
|
|
- //[_audioPlayer play];
|
|
|
+ [_audioPlayer play];
|
|
|
_isBackgroundType = YES;
|
|
|
}
|
|
|
|
|
|
/// 停止播放声音
|
|
|
- (void)stopPlayAudioSession{
|
|
|
NSLog(@"%@ stopPlayAudioSession",NSStringFromClass([self class]));
|
|
|
- //[_audioPlayer stop];
|
|
|
+ [_audioPlayer stop];
|
|
|
_isBackgroundType = NO;
|
|
|
}
|
|
|
|
|
@@ -106,20 +119,23 @@
|
|
|
|
|
|
[self stopPlayAudioSession];
|
|
|
[self.timer invalidate];
|
|
|
- [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
|
|
|
+
|
|
|
|
|
|
//设置后台模式和锁屏模式下依然能够播放
|
|
|
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
|
|
|
|
|
|
- //[[AVAudioSession sharedInstance] setActive: NO error: nil];
|
|
|
+ [[AVAudioSession sharedInstance] setActive:NO error: nil];
|
|
|
+ [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
|
|
|
+ self.backgrounTask = UIBackgroundTaskInvalid;
|
|
|
}
|
|
|
|
|
|
#pragma mark - 定时器设置判断后台保活时间,如果将要被后台杀死,重新唤醒
|
|
|
- (void)applyToSystemForMoreTime {
|
|
|
+ //HLog(@"我还在后台运行 哈哈");
|
|
|
if ([UIApplication sharedApplication].backgroundTimeRemaining < 30.0) {//如果剩余时间小于30秒
|
|
|
- [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
|
|
|
+ [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
|
|
|
self.backgrounTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
|
|
|
- [[UIApplication sharedApplication] endBackgroundTask:self.self.backgrounTask];
|
|
|
+ [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
|
|
|
self.backgrounTask = UIBackgroundTaskInvalid;
|
|
|
}];
|
|
|
}
|