我正在尝试使用播放器创建一个简单的iOS应用。有一个按钮,当您单击该按钮时,它将播放流。我的代码如下:
@IBAction func playVideo(_ sender: UIButton) {
let url = URL(string: "https://.../index.m3u8")!
// Create the asset instance and the resouce loader because we will be asked
// for the license to playback DRM protected asset.
let asset = AVURLAsset(url: url)
let queue = DispatchQueue(label: "FP License Acquire")
asset.resourceLoader.setDelegate(self, queue: queue)
// Create the player item and the player to play it back in.
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
player.play()
}
}
但是我在“ asset.resourceLoader.setDelegate(self,queue:queue)”行中收到以下错误消息,错误消息是:
"Cannot convert value of type 'ViewController' to expected argument type 'AVAssetResourceLoaderDelegate?' Insert ' as! AVAssetResourceLoaderDelegate'"
当我插入'as! AVAssetResourceLoaderDelegate”,它给出以下运行时错误:
Could not cast value of type 'DRMTest.ViewController' (0x1009ae0c8) to 'AVAssetResourceLoaderDelegate' (0x1126ebf00).
/我真的是Swift和iOS开发的新手。我不了解问题,也不知道该怎么办。我只需要一个简单的AVPlayer
答案 0 :(得分:1)
您的视图控制器应遵循AVAssetResourceLoaderDelegate
委托来解决该错误,因此代码应类似于
class ViewController: UIViewController, AVAssetResourceLoaderDelegate {
@IBAction func playVideo(_ sender: UIButton) {
let url = URL(string: "https://.../index.m3u8")!
// Create the asset instance and the resouce loader because we will be asked
// for the license to playback DRM protected asset.
let asset = AVURLAsset(url: url)
let queue = DispatchQueue(label: "FP License Acquire")
asset.resourceLoader.setDelegate(self, queue: queue)
// Create the player item and the player to play it back in.
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
present(controller, animated: true) {
player.play()
}
}
}