AVPlayer 播放受保护的 HLS,令牌过期如何更新?
Posted
技术标签:
【中文标题】AVPlayer 播放受保护的 HLS,令牌过期如何更新?【英文标题】:AVPlayer playing protected HLS, how to update token when it expires? 【发布时间】:2021-12-28 13:39:06 【问题描述】:我正在播放受保护的 HLS 流,而我的令牌在 5 分钟后过期,因此每次令牌过期时流都会下降,并且我收到错误 401 Unauthorized。问题是如何更新资产内的代币?我已经尝试在 resourceLoader loadingRequest.allHTTPHeaderFields 中更改它,但没有帮助:
func resourceLoader(_ resourceLoader: AVAssetResourceLoader,
shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool
DispatchQueue.main.async [unowned self] in
var request = loadingRequest.request
let service = apiService as! APIServiceImplementation
service.networkService.resfreshToken result in
let data = apiService.prepareForLiveRequest(url: request.url!)
request.url = data.url
var mergedHeaders = request.allHTTPHeaderFields
mergedHeaders!["Cookie"] = (data.1.values.first! as! [String: String])["Cookie"]
request.allHTTPHeaderFields = mergedHeaders
URLSession(configuration: URLSessionConfiguration.default).dataTask(with: request)
data, response, error in
if let error = error
print(error)
loadingRequest.finishLoading(with: error)
else
loadingRequest.dataRequest?.respond(with: data!)
loadingRequest.finishLoading()
.resume()
return true
【问题讨论】:
【参考方案1】:在 AVPlayerItem 里面没有办法更新 AVAsset,你唯一能做的就是使用本地服务器,很好的例子是https://cocoapods.org/pods/HLSCachingReverseProxyServer
【讨论】:
以上是关于AVPlayer 播放受保护的 HLS,令牌过期如何更新?的主要内容,如果未能解决你的问题,请参考以下文章
AVPlayer 在在线模式下停止播放 AES 加密的离线 HLS 视频