gpt4 book ai didi

ios - 后台播放音乐 : AVAudioSessionInterruptionNotification not fired

转载 作者:技术小花猫 更新时间:2023-10-29 10:54:12 27 4
gpt4 key购买 nike

我的应用在后台运行良好。 暂停播放 也运行良好,即使我的应用在后台使用为 AVAudioSessionInterruptionNotification 注册的方法。

问题到达场景的步骤:

  1. 启动我的应用程序 -> 从我的应用程序开始播放背景音乐
  2. 启动苹果的音乐应用程序并播放。 AVAudioSessionInterruptionNotification 已触发
  3. 当我暂停音乐应用程序或终止音乐应用程序时。 AVAudioSessionInterruptionNotification 不会被触发

代码:我在 appdelegate 中这样做。 didfinishlaunching

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
[[AVAudioSession sharedInstance] setActive:YES error: nil];

self.player = [[AVQueuePlayer alloc] initWithURL:[NSURL URLWithString:@"http://someurl.com:8040/;stream.mp3"]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAudioSessionEvent:) name:AVAudioSessionInterruptionNotification object:nil];




- (void) onAudioSessionEvent: (NSNotification *) notification
{
//Check the type of notification, especially if you are sending multiple AVAudioSession events here
NSLog(@"Interruption notification name %@", notification.name);

if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
NSLog(@"Interruption notification received %@!", notification);

//Check to see if it was a Begin interruption
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
NSLog(@"Interruption began!");

} else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
NSLog(@"Interruption ended!");
//Resume your audio
NSLog(@"Player status %i", self.player.status);
// Resume playing the audio.
[self.player play];

}
}
}

最佳答案

我在中断方面遇到了类似的问题。 iOS: Siri not available does not return AVAudioSessionInterruptionOptionShouldResume

最终改用 applicationWillResignActive 和 applicationDidBecomeActive。

关于ios - 后台播放音乐 : AVAudioSessionInterruptionNotification not fired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com