jai*_*ani 11 ios avaudiosession
我的应用程序在后台 播放得很好.即使我的应用程序是使用注册的方法的后台,暂停和播放也很有效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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在中断方面也遇到了类似的问题。iOS:Siri 不可用不会返回 AVAudioSessionInterruptionOptionShouldResume
最终使用 applicationWillResignActive 和 applicationDidBecomeActive 代替。
| 归档时间: |
|
| 查看次数: |
3672 次 |
| 最近记录: |