关于后台播放导致代理控制器被强应用的问题解决

#pragma mark - 设置锁频播放时显示的信息 ----
- (void)setLockScreenPlayingInfo:(NSNumber *)playbackRate {
    if (self.courseIndex >= self.courseModel.courses.count) {
        return;
    }
// 下面构建锁屏信息时,需使用弱应用weakSelf,负责会导致内存泄露
    __weak typeof(self) weakSelf = self;
    MPMediaItemArtwork *artWork = [[MPMediaItemArtwork alloc] initWithBoundsSize:CGSizeMake(100, 100) requestHandler:^UIImage *_Nonnull (CGSize size) {
        return weakSelf.iconImage;
    }];
    MTCourse *course = weakSelf.courseModel.courses[weakSelf.courseIndex];
    NSDictionary *artDic = @{ MPMediaItemPropertyTitle: course.spot.spotName,
                              MPMediaItemPropertyArtist: course.spot.countryName,
                              MPMediaItemPropertyArtwork: artWork,
                              MPMediaItemPropertyPlaybackDuration: @(weakSelf.voicePlayManager.audioPlayer.duration),
                              MPNowPlayingInfoPropertyElapsedPlaybackTime: @(weakSelf.voicePlayManager.audioPlayer.currentTime),
                              MPNowPlayingInfoPropertyPlaybackRate: playbackRate};
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:artDic];

    //播放
    MPRemoteCommand *playCommand = [MPRemoteCommandCenter sharedCommandCenter].playCommand;
    [playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    
    //暂停
    MPRemoteCommand *pauseCommand = [MPRemoteCommandCenter sharedCommandCenter].pauseCommand;
    [pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容