【问题标题】:AVPlayer is not playing a locally saved video fileAVPlayer 没有播放本地保存的视频文件
【发布时间】: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()有错误。

【问题讨论】:

    标签: ios swift video avplayer


    【解决方案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()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多