我正在尝试为 tvOS 中的视频播放器应用程序构建自定义进度条,并希望在用户扫描视频时显示视频的缩略图。
我正在使用AVPlayer
Fairplay HLS 来播放远程视频文件。我尝试使用 2 种方法来做到这一点。一个带有AVAssetImageGenerator
's copyCGImage
,另一个带有AVPlayerItemVideoOutput
'copyPixelBuffer
方法。两者都返回零。
当我尝试使用本地视频文件时,第一种方法有效。
方法一:
let imageGenerator = AVAssetImageGenerator(asset: playerItem.asset)
let progressSeconds = playerItem.duration.seconds * Double(progress)
let time = CMTime(seconds: progressSeconds, preferredTimescale: 5)
if let imageRef = try? imageGenerator.copyCGImage(at: time, actualTime: nil) {
image = UIImage(cgImage:imageRef)
}
方法二:
let videoThumbnailsOutput = AVPlayerItemVideoOutput(pixelBufferAttributes: [String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_32BGRA)])
player?.currentItem?.add(videoThumbnailsOutput)
if let pixelBuffer = videoThumbnailsOutput.copyPixelBuffer(forItemTime: time, itemTimeForDisplay: nil) {
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
有什么想法我做错了什么还是有其他方法?
谢谢!