【发布时间】:2020-12-19 09:42:27
【问题描述】:
我有这段代码试图播放以前保存的文件(存在),但我在播放视频时看到黑屏。
static func playVideo(from filename: String, vc: UIViewController, view: UIView) -> Bool {
let filepath = videosDirectoryPath + filename
if (MediaUtils.fileExists(filePath: filepath)) {
Logging.logError("Playing video failed, file not found: \(filepath)")
return false
}
let player = AVPlayer(url: URL(fileURLWithPath: filepath))
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
return true
}
我确实看到了一个错误Unbalanced calls to begin/end appearance transitions for <AVPlayerViewController: 0x109010e00>.,但不确定这是否与这里有关。我可以用这个很好地播放一个 URL:
static func playVideo(videoUrl: String, vc: UIViewController, view: UIView) {
Logging.logDebug("Playing video \(videoUrl)")
let player = AVPlayer(url: URL(string: videoUrl)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = view.frame
view.addSubview(playerViewController.view)
vc.present(playerViewController, animated: true) {
player.play()
}
}
知道为什么无法播放本地保存的文件。希望player.play()有错误。
【问题讨论】: