iOS13廢棄MPMoviePlayerViewController,MWPhotoBrowser

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播放視頻了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容