AVPlayer播放受保护的HLS,令牌过期如何更新?

AVPlayer playing protected HLS, how to update token when it expires?

我正在播放受保护的 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
        }

无法更新 AVPlayerItem 中的 AVAsset,你唯一能做的就是使用本地服务器,很好的例子是 https://cocoapods.org/pods/HLSCachingReverseProxyServer