繁体   English   中英

AVPlayer 没有播放本地保存的视频文件

[英]AVPlayer is not playing a locally saved video file

我有这段代码试图播放以前保存的文件(存在),但我在播放视频时看到黑屏。

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()有错误。

你可以试一试

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()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM