MPRemoteCommandCenter导致的偶发性闪退

2019-02-13  本文已影响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;

   }];

上一篇 下一篇

猜你喜欢

热点阅读