【问题标题】:Swift - AVPlayer load failed with error Error Domain=NSURLErrorDomain Code=-999 "cancelled"Swift - AVPlayer 加载失败并出现错误错误域 = NSURLErrorDomain 代码 = -999 “已取消”
【发布时间】:2019-04-22 16:04:02
【问题描述】:

我尝试从 url 字符串播放视频。但是问题标题出现了一些错误。

我在下面尝试此代码。 videoPath 是一个 url 字符串。

let videoURL = URL(string: videoPath)
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

以下是错误日志:

加载失败并出现错误错误域=NSURLErrorDomain 代码=-999 “取消” UserInfo={NSErrorFailingURLStringKey=http://b...a.mp4, NSErrorFailingURLKey=http://b...a.mp4, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask ."), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask ., NSLocalizedDescription=取消} [-999]

我设置了 info.plist --

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>www.example.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>

注意: 10 分钟长的视频会出现问题。正常吗?

【问题讨论】:

  • 如果videURL返回一个有效的url,你能分享一下吗?
  • 有效。当您尝试使用浏览器打开此 url 时。视频显示和播放
  • 你能说是什么解决了它吗?
  • 当我开始使用 urlAsset.resourceLoader.preloadsEligibleContentKeys = true 和流媒体 CMAF 播放列表时,我开始得到这个。但是我意识到它只发生在模拟器上,而不是设备上。对我来说,这与 AVPlayer 无关,因为这发生在一个甚至还没有实例化电影播放器​​的屏幕上。

标签: ios swift avplayer audiotoolbox


【解决方案1】:

您是否要在viewDidLoad 中显示AVPlayerViewController?如果是这样,您可以在将视图控制器的视图添加到窗口层次结构后尝试呈现它 - 例如viewDidAppear。请记住,当您导航回控制器时将调用viewDidAppear,并且将再次触发模态演示。

【讨论】:

  • 在 viewDidAppear 中
【解决方案2】:

URL 是否需要设置 cookie?我也遇到了缺少 cookie 的问题。 您可以通过尝试在隐身窗口中打开网址来进行检查。如果它仍然可以正常播放,那么也许您可以通过 -

进行调试

使用 URL 创建一个 AVURLAsset 对象,例如 -
AVURLAsset(url: &lt;URL&gt;, options:[]),并将 resourceLoader 委托设置为 self。 喜欢urlAsset?.resourceLoader.setDelegate(self, queue: .main) 并实现功能

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForRenewalOfRequestedResource renewalRequest: AVAssetResourceRenewalRequest) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel loadingRequest: AVAssetResourceLoadingRequest) {

}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, shouldWaitForResponseTo authenticationChallenge: URLAuthenticationChallenge) -> Bool {
    return true
}

func resourceLoader(_ resourceLoader: AVAssetResourceLoader, didCancel authenticationChallenge: URLAuthenticationChallenge) {

}

但如果它确实需要 cookie - 将它们设置在 AVURLAsset 对象中,例如 let asset = AVURLAsset(url: videoURL, options: ["AVURLAssetHTTPHeaderFieldsKey": ["Cookie": "<YOUR TOKEN>"]])

【讨论】:

    【解决方案3】:

    原因可能是视频中的元数据错误。看看我回答的这个帖子:AVPlayer HLS live stream IOS

    转码后的视频需要profile baseline才能在AVPlayer中播放。详细看ffmpeg转码命令:

    https://gist.github.com/chung-nguyen/d88e73e3cc8788878f5ffb8c232b4729

    【讨论】:

      【解决方案4】:

      NSErrorFailingURLKey=http://b...a.mp4 无论您拥有什么 ATS,带有“http”前缀的 url 总是会失败。 请访问我的回答here

      【讨论】:

        猜你喜欢
        • 2017-03-14
        • 1970-01-01
        • 2021-10-24
        • 2017-03-19
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 2020-02-22
        • 2015-11-14
        相关资源
        最近更新 更多