AVAudioSession 实例在处理 AVAudioSessionInterruptionNotification 结束信号后立即停用

问题描述 投票:0回答:1

我正在尝试在 Theos 中为 iOS 应用程序编写一个调整,以在后台播放无声 wav 以获得更多背景生活。可能有来电打断声音播放,所以我编写了以下通知处理。应该可以了,音频播放器可以重新启动,但是播放声音的时间很短,大约0.3秒。然后播放器将被停用并停止。

我已经尝试插入很多很多钩子日志,仍然没有找到任何线索。有人帮我吗?我非常感激。我是开发 iOS 调整的新手。有没有一些工具可以调试这个问题?

// Register interruption handler
[[NSNotificationCenter defaultCenter]  addObserver:self  
                                          selector:@selector(audioSessionInterruption:)  
                                          name:AVAudioSessionInterruptionNotification  
                                          object:nil];

- (void)audioSessionInterruption:(NSNotification *)notification{
   NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
   NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

   switch (interruptionType.unsignedIntegerValue) {
      case AVAudioSessionInterruptionTypeBegan:{
         NSLog(@"AVAudioSessionInterruptionTypeBegan");
      } break;
      case AVAudioSessionInterruptionTypeEnded:{
         if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
               // Here should continue playback. Active audio session and play
               // Unfortunately, the audio player playing less one second, and be deactived by something
               NSLog(@"AVAudioSessionInterruptionTypeEnded");
               [[AVAudioSession sharedInstance] setActive:YES error:nil];
               [self.audioPlayer play];
         }
      } break;
      default:
         break;
   }
}

查看日志,发现AudioSession激活成功,但很快就被停用了。

CAReportingClient.mm:508   message {API = "[AVAudioSession etActive:activate]";    ElapsedTime = "6.517041";}: (176093659500)
CAReportingClient.mm:508   message {API = "-[AVAudioSession ategory]";    ElapsedTime = "0.158375";}: (176093659500)
CAReportingClient.mm:508   message {API = "-[AVAudioSession privateSetCategoryWithOptions:modes:routeSharingPolicy:options:]";    ElapsedTime = "5.618958";}: (176093659500)
AVAudioSession_iOS.mm:1271  Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.
CAReportingClient.mm:508   message {API = "[AVAudioSession setActive:deactivate]";    ElapsedTime = "656.548541";}: (176093659500)

我想找出哪个进程停用了音频播放器,并避免它或编写一个钩子来处理它。这个IOS应用程序中可能还有另一个通知中断处理程序吗?

ios notifications theos tweak
1个回答
0
投票

终于找到解决办法了。中断结束后,音频会话类别选项发生了变化。解决方法是延迟 3 秒然后改回来

- (void)audioSessionInterruption:(NSNotification *)notification{
   NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
   NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

   switch (interruptionType.unsignedIntegerValue) {
      case AVAudioSessionInterruptionTypeBegan:{
         // • Audio has stopped, already inactive
         // • Change state of UI, etc., to reflect non-playing state
         NSLog(@"AVAudioSession interruption type began");
      } break;
      case AVAudioSessionInterruptionTypeEnded:{
         // • Make session active
         // • Update user interface
         // • AVAudioSessionInterruptionOptionShouldResume option
         if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
               // Here you should continue playback.
               NSLog(@"AVAudioSession interruption type ended");
               // Something changed audio session category options, here delay 3 seconds and change it back
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                  NSLog(@"Resume silence audio play from interruption");
                  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback 
                                                   withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
                  [[AVAudioSession sharedInstance] setActive:YES error:nil];
                  [self.audioPlayer play];
               });
         }
      } break;
      default:
         break;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.