iOS 10 HLS 404播放列表错误处理

iOS 10 HLS 404播放列表错误处理,ios,http-status-code-404,http-live-streaming,Ios,Http Status Code 404,Http Live Streaming,在iOS 10中,如果m3u8主播放列表请求返回404,则AVPlayerItem上的状态属性不会转换为失败状态(AVPlayerItemStatusFailed)。这会使错误处理程序无法按照 有人找到了替代方法吗?如果您的意思是URL返回404代码状态,而不是在播放或添加项目之前,您可以使用包含URL的请求进行URLSession,然后可以检查返回的响应。如果它的状态代码是200,这意味着它可以运行,如果它的值是404,那么它就有故障。 Swift 3中的示例 let reques

在iOS 10中,如果m3u8主播放列表请求返回404,则AVPlayerItem上的状态属性不会转换为失败状态(AVPlayerItemStatusFailed)。这会使错误处理程序无法按照


有人找到了替代方法吗?

如果您的意思是URL返回404代码状态,而不是在播放或添加项目之前,您可以使用包含URL的请求进行URLSession,然后可以检查返回的响应。如果它的状态代码是200,这意味着它可以运行,如果它的值是404,那么它就有故障。

Swift 3中的示例

    let request = NSMutableURLRequest(url: URL(string: "http://yourstringhere.com")!)
            request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in
        //handle if error != nil and if response.statusCode is 200 or 404
    }

    task.resume()
Swift 4解决方案 您可以跟踪AVPlayerItemNewErrorLogEntry通知。对于404错误,以下代码将打印“HTTP 404:未找到文件”

此外,本视频的后半部分还提供了有关客户端错误处理最佳实践的有用信息

NotificationCenter.default.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer?.currentItem, queue: .main) { [weak self] _ in
        print(avPlayer.currentItem?.errorLog()?.events.last?.errorComment)
}