iOS13廢棄了MPMoviePlayerViewController,導(dǎo)致使用MWPhotoBrowser播放視頻時(shí)會(huì)閃退,可以使用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) {
//當(dāng)前播放的時(shí)間
NSTimeInterval current = CMTimeGetSeconds(time);
//視頻的總時(shí)間
NSTimeInterval total = CMTimeGetSeconds(weakSelf.currentVideoPlayerViewController.player.currentItem.duration);
//輸出當(dāng)前播放的時(shí)間
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];
}
這樣就可以繼續(xù)使用MWPhotoBrowser播放視頻了