视频后台播放,上一集和下一集出现偶发性闪退
源代码如下:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
commandCenter.previousTrackCommand.enabled = YES;
[commandCenter.previousTrackCommandaddTarget:selfaction:@selector(goPrevious)];
commandCenter.nextTrackCommand.enabled = YES;
[commandCenter.nextTrackCommandaddTarget:selfaction:@selector(goNext)];
- (void)goNext{
if (_containerView.customAction) {
_containerView.customAction(_containerView, cat_next);
}
}
- (void)goPrevious{
if (_containerView.customAction) {
_containerView.customAction(_containerView, cat_previous);
}
}
使用block方法,优化代码如下:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
//上一集
commandCenter.previousTrackCommand.enabled = YES;
[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
@strongify(self);
if (self.containerView.customAction) {
self.containerView.customAction(self.containerView, cat_previous);
}
return MPRemoteCommandHandlerStatusSuccess;
}];
//下一集
commandCenter.nextTrackCommand.enabled = YES;
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
@strongify(self);
if (self.containerView.customAction) {
self.containerView.customAction(self.containerView, cat_next);
}
return MPRemoteCommandHandlerStatusSuccess;
}];
网友评论