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



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

然而,当视频开始播放时,它是一个黑屏,但音频播放正常。有什么快速解决办法吗?我使用的是12.2版本的Xcode。任何建议都将不胜感激,谢谢:(

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

欺骗性地,Quicktime"。mov";format不是视频编解码器,而是音频/视频容器格式,可以在任何数量的压缩编解码器(h.264、mpeg2、ProRes、MJPEG、AAC、mp3等(中包含视频和音频(以及其他一些东西(…您的文件不起作用,因为它们包含用iOS不支持的编解码器压缩的视频。

来自@alexkent在另一篇帖子上的SO回答。

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

最新更新