Swift 应用上的 AVPlayer 显示黑屏但在播放视频时播放音频

AVPlayer on Swift app shows a black screen but plays audio when playing video

Swift 的新手!我试图让应用程序在按下按钮时播放我的资源中的视频。我的 ViewController:

中有这个方法
if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        
        // Create a new AVPlayerViewController and pass it a reference to the player.
            let controller = AVPlayerViewController()
            controller.player = player

            // Modally present the player and call the player's play() method when complete.
        self.present(controller, animated: true) {
                player.play()
            }
    }

但是,当视频开始播放时,是黑屏,但音频播放正常。有什么快速修复方法吗?我正在使用 Xcode 版本 12.2。任何建议将不胜感激谢谢:)

问题出在视频文件本身/编码上。

Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.

来自@alexkent

我认为 .mov 不一定不适合您,但您必须确保其编码方式与 iOS 兼容。因此,正如您在评论中提到的那样,使用 .mp4 可确保视频以 iOS 兼容的方式进行编码。