use*_*249 26 ios avplayer avplayerlayer ios7
我正在AVPlayer
用于音乐播放.我的问题是,在来电后,播放器将无法恢复.来电时如何处理此问题?
小智 56
从iOS 6中,你必须处理起AVAudioSessionInterruptionNotification
和AVAudioSessionMediaServicesWereResetNotification
,这一点,你必须使用委托方法之前.
首先,您应该调用AVAudioSession单例并将其配置为您想要的用途.
例如:
AVAudioSession *aSession = [AVAudioSession sharedInstance];
[aSession setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionAllowBluetooth
error:&error];
[aSession setMode:AVAudioSessionModeDefault error:&error];
[aSession setActive: YES error: &error];
Run Code Online (Sandbox Code Playgroud)
那么你应该实现两个方法,用于AVAudioSession将调用的通知:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAudioSessionInterruption:)
name:AVAudioSessionInterruptionNotification
object:aSession];
Run Code Online (Sandbox Code Playgroud)
第一个是因为来电,闹钟等而被调用的任何中断.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleMediaServicesReset)
name:AVAudioSessionMediaServicesWereResetNotification
object:aSession];
Run Code Online (Sandbox Code Playgroud)
第二个如果媒体服务器因任何原因重置,您应该处理此通知以重新配置音频或进行任何内务处理.顺便说一句,通知字典将不包含任何对象.
以下是处理播放中断的示例:
- (void)handleAudioSessionInterruption:(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
} break;
case AVAudioSessionInterruptionTypeEnded:{
// • Make session active
// • Update user interface
// • AVAudioSessionInterruptionOptionShouldResume option
if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
// Here you should continue playback.
[player play];
}
} break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,当可选值为时,您应该恢复播放 AVAudioSessionInterruptionOptionShouldResume
对于其他通知,您应该注意以下事项:
- (void)handleMediaServicesReset {
// • No userInfo dictionary for this notification
// • Audio streaming objects are invalidated (zombies)
// • Handle this notification by fully reconfiguring audio
}
Run Code Online (Sandbox Code Playgroud)
问候.
AVAudioSession将在中断开始和结束时发送通知.请参阅处理音频中断
- (id)init
{
if (self = [super init]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
}
}
- (void)audioSessionInterrupted:(NSNotification *)notification
{
int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
if (_state == GBPlayerStateBuffering || _state == GBPlayerStatePlaying) {
NSLog(@"Pausing for audio session interruption");
pausedForAudioSessionInterruption = YES;
[self pause];
}
} else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
if ([notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue] == AVAudioSessionInterruptionOptionShouldResume) {
if (pausedForAudioSessionInterruption) {
NSLog(@"Resuming after audio session interruption");
[self play];
}
}
pausedForAudioSessionInterruption = NO;
}
}
Run Code Online (Sandbox Code Playgroud)