美文网首页
AVAudioPlayer被facetime打断不能恢复问题

AVAudioPlayer被facetime打断不能恢复问题

作者: 梦痕klrmd | 来源:发表于2017-08-12 14:59 被阅读0次

    原来的代码:

    -(void)interrupt:(NSNotification *)noti {
        int type = [noti.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
        if (type == AVAudioSessionInterruptionTypeBegan) {
            // 打断
        } else {
            // 恢复
            AVAudioSessionInterruptionOptions options = [noti.userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
            if (options == AVAudioSessionInterruptionOptionShouldResume) {
                [audioPlayer play];
            }
        }
    }
    

    原因:AVAudioSession没有设置后台播放。

    当挂断facetime时,FaceTime界面还没有消失的时候应用就已经接收到恢复通知,此时应用在后台,虽然执行了[audioPlayer play];,由于没有设置后台播放,音乐并不会播放。
    添加如下代码,设定AVAudioSession为后台播放

        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    

    这时挂断facetime时,音乐能够正常恢复播放。

    相关文章

      网友评论

          本文标题:AVAudioPlayer被facetime打断不能恢复问题

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