如何获取AVPlayer的特定HTTP错误

时间:2018-08-23 00:13:17

标签: ios swift avplayer

我开发了一个Swift iOS应用程序,可以播放实时视频传输。该服务器不受我的控制。

服务器会话可能在特定时间段后到期,因此我必须再次登录。发生这种情况时,服务器将发送HTTP代码302。问题是,尽管我可以得到错误NSURLErrorHTTPTooManyRedirects,但我无法获得引起此错误的原始302。另外,由于AVPlayer尝试,我在20秒后收到NSURLErrorHTTPTooManyRedirects,但是原始错误是即时的。

我的代码:

func play(url: URL) {
    let cookiesArray = HTTPCookieStorage.shared.cookies!
    let values = HTTPCookie.requestHeaderFields(with: cookiesArray)
    let cookieArrayOptions = ["AVURLAssetHTTPHeaderFieldsKey": values]
    let assets = AVURLAsset(url: url, options: cookieArrayOptions)
    let item = AVPlayerItem(asset: assets)
    player.replaceCurrentItem(with: item)

    // Add observer for AVPlayer status and AVPlayerItem status
    player.addObserver(self, forKeyPath: #keyPath(AVPlayer.status), options: [.new, .initial], context: nil)
    player.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem.status), options:[.new, .initial], context: nil)

    // Watch notifications
    NotificationCenter.default.addObserver(self, selector: #selector(self.newErrorLogEntry(notification:)), name: .AVPlayerItemNewErrorLogEntry, object: player.currentItem)
    NotificationCenter.default.addObserver(self, selector: #selector(self.failedToPlayToEndTime(notification:)), name: .AVPlayerItemFailedToPlayToEndTime, object: player.currentItem)

    player.play()
}

// Observe If AVPlayerItem.status Changed to Fail
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
    if object is AVPlayer, let newStatusAsNumber = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
        if #keyPath(AVPlayer.status) == keyPath && newStatusAsNumber.intValue == AVPlayerStatus.failed.rawValue {
            NSLog("observeValue.AVPlayer.status:Error: \(self.player.error?.localizedDescription ?? "Sin descripción"), error: \(self.player.error.debugDescription ?? "Sin error")")
        } else if #keyPath(AVPlayer.currentItem.status) == keyPath && newStatusAsNumber.intValue == AVPlayerItemStatus.failed.rawValue {
            // This get called after 20 seconds
            NSLog("observeValue.AVPlayer.currentItem.status:Error: \(self.player.currentItem?.error?.localizedDescription ?? "Sin descripción"), error: \(self.player.currentItem?.error.debugDescription ?? "Sin error")")
        }
    }
}

// Never called
@objc func newErrorLogEntry(notification: Notification) {
    if let object = notification.object, let playerItem = object as? AVPlayerItem, let errorLog = playerItem.errorLog() {
        NSLog("newErrorLogEntry:Error: \(errorLog)")
    }
}

// Never called
@objc func failedToPlayToEndTime(notification: Notification) {
    let error = notification.userInfo!["AVPlayerItemFailedToPlayToEndTimeErrorKey"] as? NSError
    NSLog("failedToPlayToEndTime:Error: \(error?.localizedDescription ?? "Sin descripción"), error: \(error.debugDescription ?? "Sin Error")")
}

这是我立即收到的错误,但我不知道如何捕获它:

  

任务。<1>完成并出现错误-   代码:-1007

这是我在20秒后重试的错误:

  

[] IndexFileHandleLongDownloadTimer:20秒内初始索引文件无响应

     

2018-08-22 20:36:13.213040-0300 watchValue.AVPlayer.currentItem.status:错误:HTTP太多   重定向,错误:可选(错误域= NSURLErrorDomain代码= -1007   “ HTTP重定向太多” UserInfo = {NSLocalizedDescription =太多   HTTP重定向,NSUnderlyingError = 0x604000057c10 {Error   Domain = NSOSStatusErrorDomain Code = -1007“(null)”}})

我是iOS开发的新手,欢迎任何帮助。如果您认为有更好的方法,请告诉我。谢谢!

0 个答案:

没有答案