美文网首页
MPRemoteCommandCenter导致的偶发性闪退

MPRemoteCommandCenter导致的偶发性闪退

作者: 一条鱼的星辰大海 | 来源:发表于2019-02-13 14:05 被阅读0次

视频后台播放,上一集和下一集出现偶发性闪退

源代码如下:

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;

   }];

相关文章

网友评论

      本文标题:MPRemoteCommandCenter导致的偶发性闪退

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