美文网首页
iOS13废弃MPMoviePlayerViewControll

iOS13废弃MPMoviePlayerViewControll

作者: AnxietyLane | 来源:发表于2019-12-16 13:32 被阅读0次

iOS13废弃了MPMoviePlayerViewController,导致使用MWPhotoBrowser播放视频时会闪退,可以使用AVPlayerViewController 来替换MPMoviePlayerViewController控件

首先找到MWPhotoBrowser_Private.h文件,引入头文件#import<AVKit/AVPlayerViewController.h>

#import <AVKit/AVPlayerViewController.h>

//将MPMoviePlayerViewController *_currentVideoPlayerViewController 替换
@property (nonatomic, strong) AVPlayerViewController *currentVideoPlayerViewController;

再找到MWPhotoBrowser.m文件
修改 - (void)_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index;方法

- (void)_playVideo:(NSURL *)videoURL atPhotoIndex:(NSUInteger)index {
    // Setup player
    _currentVideoPlayerViewController = [[AVPlayerViewController alloc] init];
    _currentVideoPlayerViewController.player = [AVPlayer playerWithURL:videoURL];
    _currentVideoPlayerViewController.view.frame = self.view.bounds;
    _currentVideoPlayerViewController.showsPlaybackControls = YES;
    if (@available(iOS 11.0, *)) {
        _currentVideoPlayerViewController.entersFullScreenWhenPlaybackBegins = YES;
    }
    // Show
    __weak __typeof(self) weakSelf = self;
    [_currentVideoPlayerViewController.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        //当前播放的时间
        NSTimeInterval current = CMTimeGetSeconds(time);
        //视频的总时间
        NSTimeInterval total = CMTimeGetSeconds(weakSelf.currentVideoPlayerViewController.player.currentItem.duration);
        //输出当前播放的时间
        NSLog(@"now %f",current);
        if (current >= total) {
            [weakSelf.currentVideoPlayerViewController dismissViewControllerAnimated:NO completion:nil];
            [weakSelf clearCurrentVideo];
        }
    }];
    [self presentViewController:_currentVideoPlayerViewController animated:NO completion:nil];
    [_currentVideoPlayerViewController.player play];
}

这样就可以继续使用MWPhotoBrowser播放视频了

相关文章

网友评论

      本文标题:iOS13废弃MPMoviePlayerViewControll

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