swift - 带有 AWS Cookie 的 AVPlayer 使用 AirPlay

标签 swift avplayer amazon-cloudfront http-live-streaming airplay

我尝试播放 CloudFront 提供的 HLS 流。

它在应用程序中运行良好。我只是在 AVURLAsset 中设置了 cookie,然后一切顺利。

当我在 AirPlay 提供的列表中选择一个设备(Apple TV)时出现问题。流没有开始。

我得到了错误:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12926), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x28312ed90 {Error Domain=NSOSStatusErrorDomain Code=-12926 "(null)"}}

我正在使用此代码来设置 cookie:
let url = URL(string: "https://YOUR_VIDEO_URL.m3u8");
var cookies = [HTTPCookie]()
    if let cookie = json["cookie"] as? [String: String] {
    for key in cookie.keys {
        let cookieField = ["Set-Cookie": "\(key)=\(cookie[key] ?? "")"]
        let cookie = HTTPCookie.cookies(withResponseHeaderFields: cookieField, for: url)
        cookies.append(contentsOf: cookie)
    }
}

// Assign cookies to AVPlayer
let values = HTTPCookie.requestHeaderFields(with: cookies)
let cookieOptions = ["AVURLAssetHTTPHeaderFieldsKey": values]
let assets = AVURLAsset(url: url, options: cookieOptions)
let item = AVPlayerItem(asset: assets)
let player = AVPlayer(playerItem: item)

最佳答案

You have to use AVAssetResourceLoaderDelegate and then make a request using AVAssetResourceLoadingRequest to be able to authenticate the chunk.


from OP's own comment

关于swift - 带有 AWS Cookie 的 AVPlayer 使用 AirPlay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57486044/

相关文章:

swift - 如何在 Swift 中将 Firestore FieldValue 解析为日期

ios - 从自定义 Tableview 部分标题获取 indexPath

iphone - 如何使用 MPMoviePlayerController 或 AVPlayer 在 iOS 上播放没有文件扩展名的电影文件?

amazon-s3 - AWS CDK CloudFront 到 S3 重定向

amazon-web-services - 以 S3 网站为源的 CloudFront 不提供压缩文件

swift - Swift 3.0 中的 do-until 循环

swift - 具有 IBInspectable 颜色属性的子类 - 无法覆盖

swift - 视频在 tvOS 中播放完后如何播放广告?

swift - 具有自定义开始/结束时间的循环 AVPlayer

amazon-web-services - 我可以直接从浏览器上传到 CloudFront 吗?