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播放视频了