我使用 Spotify iOS SDK 作为流媒体服务来播放音乐。因为我必须处理应用程序的中断,所以我已经集成了 AVAudioSession 。
请找到以下代码。
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *audioSessionError = nil;
[session setCategory: AVAudioSessionCategoryPlayAndRecord
error: &audioSessionError];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioSessionInterruptionNotification:)
name:AVAudioSessionInterruptionNotification
object:session];
-(void)audioSessionInterruptionNotification:(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]) {
seccReason = @"Interruption notification received";
//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
}
}
}
在后台测试应用程序时,我遇到了“ AUIOClient_StartIO失败”的问题,所以我在google上搜索过,发现它可能与 AVAudioSession 类别,所以我将其从 AVAudioSessionCategoryAmbient 更改为 AVAudioSessionCategoryPlayAndRecord ,但我仍然遇到了问题。
请指导我应该做些什么改变才能完成它。