我遇到AVPlayer
的问题,我正在使用Parse SDK来存储用户文件,并且想获取电影文件(.mp4)并使用AVPlayer
进行显示。直接使用解析文件网址存在问题,因此我正在尝试另一种方法:
// 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()
这在模拟器上可以正常工作,但最终在使用真实设备时没有视频播放。
有什么主意吗?
谢谢您的帮助