I am trying to write a tweak in Theos for an iOS app, to play a silent wav in background for more background life. There might be incoming calls interrupt the sound playing, so I wrote the following notification handling. It should work, the audio player can be restarted, but playing the sound for a very short time, about 0.3s. And then the player will be deactived and stopped.
我正试图在Theos中为iOS应用程序写一个调整,在背景中播放无声的wav,以获得更多的背景生活。可能会有来电中断声音播放,所以我写了下面的通知处理。它应该可以工作,音频播放器可以重新启动,但播放声音的时间很短,大约0.3秒。然后播放器将被停用。
I've already tried to insert many many hook logs, still not find any clue. Anyone help me? I am very appreciated. I am new to developing iOS tweaks. Are there some tools to debug this issue?
我已经试着插入了很多钩子日志,但仍然没有找到任何线索。有人帮我吗?我非常感激。我是开发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;
}
}
Check the logs, I found that the AudioSession was successfully activated but was promptly deactivated.
查看日志,我发现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)
I want to find out which process deactive the audio player, and avoid it or write a hook to handle it. May be there is another notification interruption handler in this IOS app?
我想知道是哪个进程使音频播放器失效,并避免它或写一个钩子来处理它。可能在这个IOS应用程序中还有另一个通知中断处理程序?
更多回答
优秀答案推荐
Finally I found the solution. Something changed audio session category options just after interruption being ended. The workaround is make a delay 3 seconds and change it back
最后我找到了解决办法。中断刚结束,音频会话类别选项就发生了变化。解决方法是延迟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;
}
}
更多回答
我是一名优秀的程序员,十分优秀!