iOS远程控制事件
2020-09-08 本文已影响0人
奶茶大叔
最近开发收到一个产品需求 :需要在用户看直播的时候,APP退到后台,锁屏界面增加远程控件、通知远程栏控件
虽然项目用的是TXLiteAVSDK,然后去官网查看文档得到这个结论:

虽然无法实现,但是还是在TXVodPlayer点播功能下试了试
1.首先需要设置项目

2.didFinishLaunchingWithOptions 方法加载
AVAudioSession *session = [AVAudioSession sharedInstance];
if (@available(iOS 10.0, *)) {
[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowAirPlay error:nil];
} else {
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
}
NSError *error;
[session setActive:YES error:&error];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
3.在需要播放的controller或者View里重写canBecomeFirstResponder,并主动调用
- (BOOL)canBecomeFirstResponder
{
return YES;
}
4.配置远程空间播放信息
- (void)configNowPlayingInfoCenter
{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//设置曲目
[dict setObject:self.playerStatus.playModel.title forKey:MPMediaItemPropertyTitle];
//设置歌手名
[dict setObject:self.playerStatus.playModel.nickname forKey:MPMediaItemPropertyArtist];
//设置歌曲时长
[dict setObject:[NSNumber numberWithDouble:self.playerStatus.playModel.live_time.doubleValue] forKey:MPMediaItemPropertyPlaybackDuration];
UIImage *img = [UIImage imageNamed:@"program_placeholder_large"];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:img];
[dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
5.接收远程控制事件
iOS11之前
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
default:
break;
}
}
iOS11之后
//远程事件监听
if(@available (iOS 11.0 ,*)){
MPRemoteCommandCenter* commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
return MPRemoteCommandHandlerStatusSuccess;
}];
}