如何解决尝试播放 drm 内容但在 avplayer 上播放
这里是我使用的代码
// 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 举报,一经查实,本站将立刻删除。