I am having a problem with AVPlayer
, I'm using Parse SDK to store user files, and I want to fetch a movie file (.mp4) and display it using AVPlayer
. There is a problem with using directly the Parse file url, so I'm trying a different approach:
// First, I load the file from the video url
// `name` is the file name fetched from Parse
URLSession.shared.dataTask(with: videoURL) { data, response, error in
DispatchQueue.main.async {
do {
// I will store locally the file to be read by AVPlayer afterwards
let filepath = FileManager.default.temporaryDirectory.appendingPathComponent(name)
try data?.write(to: filepath, options: .atomicWrite)
// I create an AVPlayerItem from the local url
let playerItem = AVPlayerItem(url: filepath as URL)
let player = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.viewController?.present(playerViewController, animated: true) {
playerViewController.player?.play()
}
} catch {
print(error)
}
}
}.resume()
This works fine with the simulator, but I end up having no video playing when using a real device.
Any idea?
Thank you for your help