AVPlayer 没有播放本地保存的视频文件
Posted
技术标签:
【中文标题】AVPlayer 没有播放本地保存的视频文件【英文标题】:AVPlayer is not playing a locally saved video file 【发布时间】: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()
有错误。
【问题讨论】:
【参考方案1】:你可以试一试?
func playVideo(videoUrl: String, vc: UIViewController, view: UIView)
let player = AVPlayer(url: NSURL(fileURLWithPath: videoUrl) as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspect
playerLayer.frame = logoImageView.frame
view.layer.addSublayer(playerLayer)
vc.present(playerViewController, animated: true)
player?.play()
【讨论】:
以上是关于AVPlayer 没有播放本地保存的视频文件的主要内容,如果未能解决你的问题,请参考以下文章