如何正确处理AVPlayer HTTP错误? [英] How to correctly handle AVPlayer HTTP error?

查看:107
本文介绍了如何正确处理AVPlayer HTTP错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当请求从Web服务器播放mp3文件时,如果该服务器返回禁止的403,则在检查AVPlayer当前项目错误时尚不清楚如何处理该错误.

When requesting to play a mp3 file from a web server, if that server returns a 403 forbidden it is not clear how to handle the error when examining the AVPlayer current item error.

来自AVPlayer的错误消息并不表示它是403 ...

Error message from AVPlayer doesn't indicate that it is a 403...

2019-01-05 13:08:33.908316-0500 Runner [84043:3269818]可选(错误 域= AVFoundationErrorDomain代码= -11828无法打开" UserInfo = {NSLocalizedFailureReason =此媒体格式不是 支持.NSLocalizedDescription =无法打开, NSUnderlyingError = 0x600000781290 {Error Domain = NSOSStatusErrorDomain Code = -12847(null)"}})

2019-01-05 13:08:33.908316-0500 Runner[84043:3269818] Optional(Error Domain=AVFoundationErrorDomain Code=-11828 "Cannot Open" UserInfo={NSLocalizedFailureReason=This media format is not supported., NSLocalizedDescription=Cannot Open, NSUnderlyingError=0x600000781290 {Error Domain=NSOSStatusErrorDomain Code=-12847 "(null)"}})

该错误表明不支持该媒体,但是甚至从未访问过该媒体.反正有没有看到AVPlayer请求中的HTTP错误代码?

The error says the media isn't supported, but the media was never even reached. Is there anyway to see the HTTP error code from AVPlayer requests?

在Android上测试同一文件时,我能够正确地看到来自Android本机MediaPlayer的403错误代码(Android的错误比iOS的AVPlayer更好,更有用).

When testing the same file on Android, I am able to correctly see a 403 error code from Android native MediaPlayer (the error from Android is better and more useful than iOS's AVPlayer).

由于缺少正确的错误消息,因此很难向用户正常显示准确的错误.

This lack of proper error messaging makes it very difficult to gracefully display an accurate error to the user.

任意代码示例:

    let url = URL(string: "some 403 server url")
    let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
    player = AVPlayer(playerItem: playerItem)
    player.play()

检查错误将打印上面引用的消息.

Checking the error will print the above quoted message.

NSLog("\(String(describing: player.currentItem?.error))")

推荐答案

这是一个可行的解决方案,而不是使用URL初始化AVPlayerItem

Here's a possible solution, Rather than initializing the AVPlayerItem with a url

您可以尝试这种方法

  1. 使用AVURLAsset并设置AVAssetResourceLoaderDelegate

使用委托方法 func resourceLoader(_ resourceLoader:AVAssetResourceLoader, didCancel authenticationChallenge: URLAuthenticationChallenge)

继续创建播放器并播放音频,当出现403错误时,委托方法将让您知道.

Proceed to create the player and play audio, The delegate method will let you know when there is a 403 error.

这是示例代码

class ResourceLoadingDelegate:NSObject, AVAssetResourceLoaderDelegate {
      func resourceLoader(_ resourceLoader: AVAssetResourceLoader,
                    didCancel authenticationChallenge:  URLAuthenticationChallenge) {
    /// handle the authentication challenge
    print(authenticationChallenge.error.debugDescription)
   }
}

   let asset = AVURLAsset(url: URL(string: "some 403 url")!)

   asset.resourceLoader.setDelegate(ResourceLoadingDelegate(), queue:  DispatchQueue.global(qos: .userInitiated))

这篇关于如何正确处理AVPlayer HTTP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆