美文网首页iOS 精美收藏iOS技术交流
iOS开发小知识之检测程序被中断,进入后台,耳机的插拔点击等事件

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

作者: 左左4143 | 来源:发表于2016-09-28 22:36 被阅读891次

    在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;
            }
        }
        
    }
    

    相关文章

      网友评论

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

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