OGeek|极客世界-中国程序员成长平台

标题: ios AVPlayerViewController 无法播放本地mp4 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:47
标题: ios AVPlayerViewController 无法播放本地mp4

我尝试使用 AVPlayerViewController 播放 mp4 视频。我用“http url”,它可以播放,但是当我使用fileUrl时,它就不起作用了……

    NSString *filePath = [[NSBundle mainBundle] pathForResource"mp4ForDownloadTest" ofType"mp4"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];

    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];

    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

    playerVC.player = player;

    [self presentViewController:playerVC animated:YES completion:^{
        [player play];
    }];
}

enter image description here



Best Answer-推荐答案


试试这个:

        guard let path = NSBundle.mainBundle().pathForResource("drum", ofType:"mp4") else {
            break
        }

        let url = NSURL(fileURLWithPath: path)
        self.loadPlayer(url) 

然后:

private func loadPlayer(url: NSURL) {
    // present video window
    let playerItem = AVPlayerItem(URL: url)
    let player = AVPlayer.init(playerItem: playerItem)
    self.playerViewController.player = player

    self.view.addSubview(self.playerViewController.view)
    self.videoControlView.alpha = 1.0
    self.setupPlayerObserver()
    self.playPlayer()
    self.setSlider()
}

关于ios AVPlayerViewController 无法播放本地mp4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36302220/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4