AVPlayerViewController doesn't play local vide

2019-03-04 00:23发布

问题:

I have added a DemoVideo.mp4 in my project and added it to copy resource file. But when i run the app it doesn't play my video. Here is my method.

private func setUpAndPlayVideo() {
        guard let videoPath = Bundle.main.path(forResource: "DemoVideo.mp4", ofType: nil) else {
            return 
        }
        let videoURL = NSURL(string: videoPath)
        let player = AVPlayer(url: videoURL! as URL)
        playerViewController = AVPlayerViewController()
        playerViewController.player = player
        playerViewController.view.frame = self.videoPlayerView.bounds
        self.videoPlayerView.addSubview(playerViewController.view)
        playerViewController.showsPlaybackControls = false
        playerViewController.player?.play()
}

I am invoking this method in viewDidAppear method but it display this.

回答1:

 let path : String = Bundle.main.path(forResource: "charlie", ofType: "mp4")!
 let movieurl : URL = URL(fileURLWithPath: path)
 let movie : AVPlayerViewController = AVPlayerViewController()
 movie.view.frame = self.view.bounds

 let player : AVPlayer = AVPlayer(url: movieurl)
 movie.player = player

 self.view.addSubview(movie.view)
 player.play()