Quellcode durchsuchen

1.后台逻辑修改(解决没有后台回来没有声音问题)

huangxiaodong vor 1 Jahr
Ursprung
Commit
5c148936ef

+ 1 - 1
创维盒子/双子星云手机/AppDelegate/AppDelegate.m

@@ -85,7 +85,7 @@
     
     
     //启动后台保活
-    //[AudioSessionObject shareManager];
+    [AudioSessionObject shareManager];
     [self MonitorNetworkChangesFun];
     
 #ifdef DEBUG

+ 1 - 3
创维盒子/双子星云手机/CloudPlayer/PlayerViewController+AppDelegate.m

@@ -68,9 +68,7 @@
     [self disconnectVideoServer];
     
     HLog(@"开始进入后台");
-    [cachesFileManager writeLogsWithMsg:@"EnterBackground"];
-    
-    [AudioSessionObject shareManager];
+    //[cachesFileManager writeLogsWithMsg:@"EnterBackground"];
 }
 
 - (void)applicationDidBecomeActive:(NSNotification *)notification

+ 28 - 12
创维盒子/双子星云手机/Vendor/BackgroundKeepAlive/AudioSessionObject.m

@@ -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;
         }];
     }