Home > Enterprise >  How to play a video from file path in AVPlayer?
How to play a video from file path in AVPlayer?

Time:04-15

I have recorded a video from my app which saves the video in Photos app in iPhone. The file path of the video saved is:

file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14@14-26-59GMT 05:00ARVideo.mp4

How can I play the video from the file path in AVPlayer?

class SecondViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    playVideo()
}

private func playVideo() {
    guard let filePath = Bundle.main.path(forResource: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14@14-26-59GMT 05:00ARVideo.mp4", ofType: "mp4") else { return}
    
    let player = AVPlayer(url: URL(fileURLWithPath: filePath))
    
    let vc = AVPlayerViewController()
    vc.player = player
    
    present(vc, animated: true)
}
}

CodePudding user response:

import AVKit

func playVideo(){
    let player = AVPlayer(url: URL(video url string) ?? URL(fileURLWithPath: "") )
    let playerController = AVPlayerViewController()
    playerController.player = player
    self.addChild(playerController)
    self.view.addSubview(playerController.view)
    playerController.view.frame = self.view.frame
    playerController.exitsFullScreenWhenPlaybackEnds = true
    player.play()
}

CodePudding user response:

class SecondViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    playVideo()
}
// if u have video in app directory/ document directory 
private func playVideo() {
 guard let url = URL(string: "file:///var/mobile/Containers/Data/Application/0A8F1D57-7336-4D52-8B64-928C381DBC4F/Documents/2022-04-14@14-26-59GMT 05:00ARVideo.mp4") 
 else {
    print("url not exist")
    return
    } 
    let player = AVPlayer(url: url)
    let vc = AVPlayerViewController()
    vc.player = player
    present(vc, animated: true)
  }

// if u have video locally that u put in ur project then
    private func playVideoFromLocal() {
        guard let fileURL = Bundle.main.url(forResource: "file_example_MOV_480_700kB", withExtension: "mov") else {return}
        let player = AVPlayer(url: fileURL)
        let playerController = AVPlayerViewController()
        let vc = AVPlayerViewController()
        vc.player = player
        present(vc, animated: true)
    }
}

CodePudding user response:

The path of the documents changes for the app each time it is launched, which changes the first part of the URL.

Try the following updated function to get the current location of the file:

private func playVideo() {
      let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
      let playerItem = AVPlayerItem(url: URL(fileURLWithPath: documentsPath.appendingFormat("/your-video-name-here.mp4")))
      let player = AVPlayer(playerItem: playerItem)
      let vc = AVPlayerViewController()
      vc.player = player
      present(vc, animated: true)
}
  • Related