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

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

作者: 扶兮摇兮 | 来源:发表于2020-04-20 20:17 被阅读0次
#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;
    }];
}

相关文章

网友评论

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

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