【问题标题】:AVPlayer on Swift app shows a black screen but plays audio when playing videoSwift 应用程序上的 AVPlayer 显示黑屏,但在播放视频时播放音频
【发布时间】:2020-12-10 17:44:52
【问题描述】:

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 版。任何建议将不胜感激谢谢:)

【问题讨论】:

标签: ios swift xcode video avplayer


【解决方案1】:

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

具有欺骗性的是,Quicktime“.mov”格式不是视频编解码器,而是音频/视频容器格式,可以包含任意数量的压缩编解码器(h.264、mpeg2、 ProRes、MJPEG、AAC、mp3 等)...您的文件无法正常工作,因为它们包含使用 iOS 不支持的编解码器压缩的视频。

来自@alexkent 在this SO answer on another post

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多