iOS锁屏控制音乐播放

后台播放音频设置,需要在Capabilities->Background Modes中勾选“Audio,Airplay,and Picture in Picture”

设置AVAudioSession支持锁屏后依然可以播放

AVAudioSession *session = [AVAudioSession sharedInstance];
// 激活
[session setActive:YES error:nil];
// 支持后台播放
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

方案1:

如果需要在锁屏界面开启和监控远程控制事件,可以通过重写- (void)remoteControlReceivedWithEvent:(UIEvent *)event这个方法来捕获远程控制事件,并根据event.subtype来判别指令意图并作出反应。

// 在具体的控制器或其它类中捕获处理远程控制事件
// 当远程控制事件发生时触发该方法, 该方法属于UIResponder类
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    NSLog(@"%s %d %d", __func__, (int)receivedEvent.type, (int)receivedEvent.subtype);
    if  (receivedEvent.type  ==  UIEventTypeRemoteControl)  {
        switch  (receivedEvent.subtype)  {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                //耳机线控播放暂停
                NSLog(@"UIEventSubtypeRemoteControlTogglePlayPause");
                break;
            case UIEventSubtypeRemoteControlPlay:
                //播放
                NSLog(@"UIEventSubtypeRemoteControlPlay");
                break;
            case UIEventSubtypeRemoteControlPause:
                //暂停
                NSLog(@"UIEventSubtypeRemoteControlPause");
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                //下一首
                NSLog(@"UIEventSubtypeRemoteControlNextTrack");
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                //上一首
                NSLog(@"UIEventSubtypeRemoteControlPreviousTrack");
                break;
            default:
                break;
        }
    }
}

方案2:

iOS7.1之后新增了MPRemoteCommandCenter、MPRemoteCommand 及其相关的一些类 ,锁屏界面开启和监控远程控制事件就更方便了,而且还扩展了一些新功能:如播放进度拖拽调节功能和菜单弹框功能。

官方文档

#import <MediaPlayer/MediaPlayer.h>


//锁屏界面开启和监控远程控制事件
- (void)addRemoteCommand {    
    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    // MPFeedbackCommand对象反映了当前App所播放的反馈状态
    // MPRemoteCommandCenter对象提供feedback对象用于对媒体文件进行喜欢, 不喜欢, 标记的操作
    
    // 添加喜欢按钮
    MPFeedbackCommand *likeCommand = commandCenter.likeCommand;
    likeCommand.enabled = YES;
    likeCommand.localizedTitle = @"喜欢";
    [likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"喜欢");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 添加不喜欢按钮
    MPFeedbackCommand *dislikeCommand = commandCenter.dislikeCommand;
    dislikeCommand.enabled = YES;
    dislikeCommand.localizedTitle = @"不喜欢";
    [dislikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"上一首");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 标记
    MPFeedbackCommand *bookmarkCommand = commandCenter.bookmarkCommand;
    bookmarkCommand.enabled = YES;
    bookmarkCommand.localizedTitle = @"标记";
    [bookmarkCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"标记");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 切换播放暂停
    [commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 暂停
    [commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [self.player pause];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 播放
    [commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        [self.player play];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 上一首
    [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"上一首");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    // 下一首
    [commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"下一首");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    //在控制台拖动进度条调节进度
    [commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        CMTime totlaTime = self.player.currentItem.duration;
        MPChangePlaybackPositionCommandEvent * playbackPositionEvent = (MPChangePlaybackPositionCommandEvent *)event;
        [self.player seekToTime:CMTimeMake(totlaTime.value*playbackPositionEvent.positionTime/CMTimeGetSeconds(totlaTime), totlaTime.timescale) completionHandler:^(BOOL finished) {
        }];
        return MPRemoteCommandHandlerStatusSuccess;
    }];
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值