浏览代码

1.改静音键开启可以播放没有成功

huangxiaodong 1 年之前
父节点
当前提交
8f1201d24c
共有 1 个文件被更改,包括 12 次插入9 次删除
  1. 12 9
      创维盒子/双子星云手机/Vendor/BackgroundKeepAlive/AudioSessionObject.m

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

@@ -52,9 +52,15 @@
     [self.timer invalidate];
     _isBackgroundType = NO;
     
-    // 恢复音频播放
-    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; // ok
-    [[AVAudioSession sharedInstance] setActive: YES error: nil];
+    // 恢复音频播放 AVAudioSessionCategoryPlayback 可以在开启静音后播放声音 但是 停止后台播放后 [[AVAudioSession sharedInstance] setActive: No error: nil] 后无声音了 AVAudioSessionCategorySoloAmbient 可以 但是开启静音后无声
+    
+    //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
+    //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];//ok
+    //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; // ok
+    //[[AVAudioSession sharedInstance] setActive: YES error: nil];
+    
+    [[AVAudioSession sharedInstance] setActive:YES  error: nil];
+    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
     
 }
 
@@ -87,8 +93,8 @@
 /// 创建音乐播放器
 - (void)creatAVAudioSessionObject{
    //设置后台模式和锁屏模式下依然能够播放
-   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
+   [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
    //初始化音频播放器
    NSError *playerError;
    NSURL *urlSound = [[NSURL alloc]initWithString:[[NSBundle mainBundle]pathForResource:@"RunInBackground" ofType:@"mp3"]];
@@ -120,11 +126,8 @@
     [self stopPlayAudioSession];
     [self.timer invalidate];
 
-    
-    //设置后台模式和锁屏模式下依然能够播放
-    //[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
-    
-    [[AVAudioSession sharedInstance] setActive:NO error: nil];
+    [[AVAudioSession sharedInstance] setActive:NO withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error: nil];
+    //[[AVAudioSession sharedInstance] setActive:NO error: nil];
     [[UIApplication sharedApplication] endBackgroundTask:self.backgrounTask];
     self.backgrounTask = UIBackgroundTaskInvalid;
 }