Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios AvPlayServiceController无法播放本地mp4_Ios_Video_Avplayerviewcontroller - Fatal编程技术网

ios AvPlayServiceController无法播放本地mp4

ios AvPlayServiceController无法播放本地mp4,ios,video,avplayerviewcontroller,Ios,Video,Avplayerviewcontroller,我尝试使用avplayervewcontroller播放mp4视频。我使用“http url”,它可以播放,但当我使用文件url时,它不起作用 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mp4ForDownloadTest" ofType:@"mp4"]; if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { NSURL

我尝试使用
avplayervewcontroller
播放mp4视频。我使用“http url”,它可以播放,但当我使用
文件url
时,它不起作用

    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];
    }];
}
试试这个:

        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()
}