美文网首页iOS 归纳总结iOS
iOS音频播放中断的处理

iOS音频播放中断的处理

作者: Maggie的小蜗居 | 来源:发表于2016-12-04 22:17 被阅读1225次

    当前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后被弃用

    相关文章

      网友评论

        本文标题:iOS音频播放中断的处理

        本文链接:https://www.haomeiwen.com/subject/ktxkmttx.html