问题描述
这里是我使用的代码
// certificate url
let certificateURL = URL(string: "https://cn-fp-certificate.s3.ap-south-1.amazonaws.com/fairplay+certificate/fairplay.cer")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// playback url
streamingURL = " https://dev-cn-ottohm-movies.s3.ap-south-1.amazonaws.com/testing-cmaf/output-fairplay-and-widevine/180000005/master.m3u8"
if let url = URL(string: streamingURL) {
//1. Create AVPlayer object
let asset = AVURLAsset(url: url)
// let queue = dispatchQueue(label: "LicenseGetQueue")
asset.resourceLoader.setDelegate(self,queue: queue)
let playerItem = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: playerItem)
print("player \(player)")
//2. Create AVPlayerLayer object
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoView.bounds //bounds of the view in which AVPlayer should be displayed
playerLayer.videoGravity = .resizeAspect
//3. Add playerLayer to view's layer
self.videoView.layer.addSublayer(playerLayer)
//4. Play Video
player.play()
}
}
func resourceLoader(_ resourceLoader: AVAssetResourceLoader,shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
//we first check if a url is set in the manifest
guard let url = loadingRequest.request.url else {
print("[ViewController] resouceLoader url",#function,"Unable to read the url/host data")
loadingRequest.finishLoading(with: NSError(domain: "com.iccaps.error",code: -1,userInfo: nil))
return false
}
print("urlloadingRequest \(url)")
print("[ViewController] resouceLoader URL",url)
//when the url is correctly found we try to load the certificate date. watch out for this
//example the certificate resides inside the bundle. but it sould be preferable fetched from the server
guard
let certificateData = try? Data(contentsOf: certificateURL!)
else {
print("[ViewController] resourceLoader certificate","Unable to read the certificate data.")
loadingRequest.finishLoading(with: NSError(domain: "com.icapps.errror",code: -2,userInfo: nil))
return false
}
print("certificatedata \(certificateData)")
// Request the Server Playback Context.
let contentId = "75a4fbcc7fade20fac3385237ec65976"
guard
let contentIdData = contentId.data(using: String.Encoding.utf8),let spcData = try? loadingRequest.streamingContentKeyRequestData(forApp: certificateData,contentIdentifier: contentIdData,options: nil),let dataRequest = loadingRequest.dataRequest else {
loadingRequest.finishLoading(with: NSError(domain: "com.icapps.error",code: -3,userInfo: nil))
print("?","Unable to read the SPC data.")
return false
}
print("dataRequest \(dataRequest)")
// Request the Content Key Context from the Key Server Module.
let ckcURL = URL(string: "https://krpca13io8.execute-api.ap-south-1.amazonaws.com/dev/encryptionKeys")!
var request = URLRequest(url: ckcURL)
request.httpMethod = "POST"
request.httpBody = spcData
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request) { data,response,error in
if let data = data {
// The CKC is correctly returned and is Now send to the `AVPlayer` instance so we
// can continue to play the stream.
dataRequest.respond(with: data)
loadingRequest.finishLoading()
print("finish to load")
print("response: \(response)")
}
else
{
print("?","Unable to fetch the CKC.")
loadingRequest.finishLoading(with: NSError(domain: "com.icapps.error",code: -4,userInfo: nil))
}
}
task.resume()
print("task resume")
return true
}
}
有知道的请回复一下,很有帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)