iOS 归纳总结iOS

iOS音频播放中断的处理

2016-12-04  本文已影响1225人  Maggie的小蜗居

当前app在播放音频,此时打开另外一个app,或者系统铃声响起,会我们的app被打断的现象,此时我们需要暂停我们的播放界面,以及其它一系列的动作,
那我们需要获取到当前打断的这个事件

系统提供了一个打断通知 供我们进行打断处理

AVAudioSessionInterruptionNotification

  1. 注册通知
[ [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterrupted:) name:AVAudioSessionInterruptionNotification object:nil];
  1. 处理通知
    
- (void)audioSessionInterrupted:(NSNotification *)notification
{
    //通知类型
    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    AVAudioSessionInterruptionOptions options =  
           [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];  
    switch (interruptionType.unsignedIntegerValue) {

           case AVAudioSessionInterruptionTypeBegan:{
            
            [self postNotification:StopMusicByInterruptNotification];
            
        } break;
        case AVAudioSessionInterruptionTypeEnded:{

            

        if (options == AVAudioSessionInterruptionOptionShouldResume) {  
            //触发重新播放

          }  
  
            [self postNotification:InterruptEndNotification];
        } break;
        default:
            break;
    }

}

也可以使用AVAudioSessionDelegate,使用的是AVAudioPlayer或AVAudioRecorder,还可以使用对应的AVAudioPlayerDelegate和 AVAudioRecorderDelegate。但AVAudioSessionDelegate在6.0后被弃用

上一篇 下一篇

猜你喜欢

热点阅读