ios音乐播放中断处理

本文介绍使用AVAudioPlayer在iOS应用中处理音频播放中断的方法。包括注册中断通知、实现不同类型的中断处理逻辑,并提出改进方案避免后台暂停时的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//这里我用的是AVAudioPlayer
- (void)viewDidLoad {
    [super viewDidLoad];

    //-->在通知中心注册一个事件中断的通知:
    //处理中断事件的通知
    //-->实现接收到中断通知时的方法
    //处理中断事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterreption:) name:AVAudioSessionInterruptionNotification object:nil];

    NSBundle *bundle=[NSBundle mainBundle];
    NSString *urlString=[bundle  pathForResource:@"陈一发儿 - 童话镇" ofType:@"mp3"];
    NSURL *url=[[NSURL alloc]initFileURLWithPath:urlString];
    AVAudioPlayer *player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    player.delegate=self;
    player.numberOfLoops=99;
    self.player=player;
}
-(void)handleInterreption:(NSNotification *)sender
{
    int type = [sender.userInfo[AVAudioSessionInterruptionOptionKey] intValue];

    switch (type) {
        case AVAudioSessionInterruptionTypeBegan: // 被打断
            NSLog(@"播放");
            //这里要写一个dowhile循环,因为如果直接写一句播放语句的话,有时候如果打电话立马挂了,多试几次,可能会不播放,所以要写循环,直到状态为播放为止;,至于为什么,我也没有搞清楚,可能是bug,也可能是我没找对方法吧
            do {
                NSLog(@"循环");
                [self.player prepareToPlay];
                [self.player play];
            } while (!self.player.playing);
            break;
        case AVAudioSessionInterruptionTypeEnded: // 中断结束
            NSLog(@"暂停");
            [self.player pause];
            break;
        default:
            NSLog(@"其他");
            break;
    }
}

2改进方案,之前一直觉得写个循环总觉得不是多么对,终于现在找到了一个问题点
在后台时,我们暂停播放不要用pause方法,用stop方法就可以了,亲测半天,果然问题出在这里。以下为改进代码:

-(void)handleInterreption:(NSNotification *)sender
{
    int type = [sender.userInfo[AVAudioSessionInterruptionOptionKey] intValue];

    switch (type) {
        case AVAudioSessionInterruptionTypeBegan: // 被打断
        {
            NSLog(@"播放");

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

//            do {
//                NSLog(@"循环");
//                [self.player prepareToPlay];
//                [self.player play];
//            } while (!self.player.playing);

            //[self.player prepareToPlay];
            [self.player play];
        }
            break;
        case AVAudioSessionInterruptionTypeEnded: // 中断结束
        {
            NSLog(@"暂停");
            [self.player stop];

             AVAudioSession *audioSession=[AVAudioSession sharedInstance];
            [audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
        }
            break;
        default:
            NSLog(@"其他");
            break;
    }
}

以上1和2注释的东西可要可不要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值