iOS开发小知识之检测程序被中断,进入后台,耳机的插拔点击等事件

在iOS端关于音频或视频的开发中,声音或者视频播放的时候一些外界的因素可能会把音视频打断,比如电话,或者其他会播放音频的APP,这时候程序的音频或视频都会中断,但是我们再回到前台时需要更改一些UI上的变化(比如正在播放的音乐被电话打断,电话挂掉之后音乐会保持暂停的状态,但是如果不及时更新UI,播放按钮会保持正在播放的状态),这时候我们就需要对这些时间进行主动的监听,来进行我们需要的操作,

情况一:用户按home键主动退到后台,一般的音乐类播放软件都会有后台播放的设置,不用进行额外的操作,但是如果是正在播放视频的话,退到后台后视频应该是暂停状态,这时我们只需要在AppDelegate的相关方法中进行主动的设置就可以

//程序即将进入后台
- (void)applicationWillResignActive:(UIApplication *)application {
    //这里可以用通知中心的方式,通知视频播放的界面将UI更新为暂停的状态(视频会自动暂停)
    [[NSNotificationCenter defaultCenter] postNotificationName:BackgroundOperation object:@"willResignActive"];
}

//程序即将进入活跃状态
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //此时程序已经回到前台,如果需要的话可以让视频继续播放,让UI切换到播放的状态,如果不需要可以什么都不做
    [[NSNotificationCenter defaultCenter] postNotificationName:BackgroundOperation object:@"didBecomeActive"];
    
}

复制代码

情况二:被电话或者其他App播放的声音打断,此时就需要我们在程序中去监听这些事件,当事件被触发时进行相应的操作

 //监测被中断的事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isPlayerPlaying:) name:AVAudioSessionInterruptionNotification object:nil];
复制代码

其中AVAudioSessionInterruptionNotification就是系统提供的被中断的通知

然后在被打断时进行相应的操作

//检测歌曲被打断事件(别的软件播放音乐,来电话)
- (void)isPlayerPlaying:(NSNotification *)notification {
    NSInteger type = [[notification.userInfo valueForKey:@"AVAudioSessionInterruptionTypeKey"] integerValue];
    if (type == 1) {
       //在这里进行你想要的操作
    }
}
复制代码

音乐类软件为了提高用户体验一般都会添加耳机线控音乐的功能(单机暂停/播放,双击下一曲,点击三下上一曲),还可以对耳机的插拔进行监听

监听耳机的插拔:

//监听耳机的插拔
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

//相应的事件
- (void)routeChange:(NSNotification *)notification {
    NSDictionary *interuptionDict = notification.userInfo;
    NSInteger roteChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    
    switch (roteChangeReason) {
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            //插入耳机
            break;
            
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            //拔出耳机
            NSLog(@"拔出耳机");
            [self pausePlay];
            
            break;

    }
    
}

//上面的roteChangeReason还有其他的集中类型,代表了其他的状态,可以对应的进行不同的操作
复制代码

点击耳机中键的事件:

首先要在程序入口处让app接收远程控制事件

//让app支持接收远程控制事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
复制代码

然后在远程事件通知中进行相应的操作(这个通知还会接收系统上拉菜单中的控制中心的播放和暂停按钮)

//app接受远程控制(控制中心 耳机等)
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {

    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case 100:
                //控制中心的播放按钮
                [[PlayingManager defaultManager] musicPlay];
                break;
            case 101:
                //控制中心的暂停按钮
                [[PlayingManager defaultManager] pausePlay];
                break;
            case 103:{
                //耳机的单击事件 根据音乐的播放状态选择暂停或播放
                if ([[PlayingManager defaultManager] getStateOfPlayer]) {
                    [[PlayingManager defaultManager] pausePlay];
                } else {
                    [[PlayingManager defaultManager] musicPlay];
                }
                
            }
                break;
            case 104:
                //耳机的双击中键事件 
                [[PlayingManager defaultManager] nextMusic];
                break;
             case 105:
                //耳机的中键盘点击三下触发的时间
                [[PlayingManager defaultManager] beforeMusic];
                break;
                
            default:
                break;
        }
    }
    
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值