gpt4 book ai didi

ios - 使用 MPMoviePlayer 请求视频缩略图的问题

转载 作者:行者123 更新时间:2023-11-28 09:13:35 24 4
gpt4 key购买 nike

我正在尝试解析包含视频 URL 和标题的 JSON 数据。解析数据后,我在 UITableviewCell 中显示视频缩略图。问题是当数据被解析时,当它请求缩略图时,NSNotification 选择器方法从未被调用。这是我的代码:

override func viewDidLoad() { 
super.viewDidLoad()
getJson_results()
}

override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

func videoThumbnailIsAvailable(notification: NSNotification) {
println("Flag1") //the control never reaches here
var thumbnail = notification.userInfo?[MPMoviePlayerThumbnailImageKey] as UIImage
self.temp_image = thumbnail
}


func videodurationIsAvailable(notification: NSNotification) {
var val = moviePlayer?.duration
self.temp_duration = val!
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return length_array;
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as WPR_Main
cell.imgView?.image = get_Media_thumbnail(NSURL(string:self.url[indexPath.row])!)
cell.titleLabel?.text = self.titles[indexPath.row]
return cell
}


func get_Media_thumbnail(url: NSURL) -> UIImage {
moviePlayer? = MPMoviePlayerController(contentURL: url)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "videoThumbnailIsAvailable:",
name: MPMoviePlayerThumbnailImageRequestDidFinishNotification,
object: nil)
moviePlayer?.requestThumbnailImagesAtTimes([1.0], timeOption:.NearestKeyFrame )
return self.temp_image
}

最佳答案

实现此目的的最佳方法是使用 AVAssetImageGenerator 而不是使用 requestThumbnailImagesAtTimes。 AVAsset 还允许异步下载图像,这非常快并且适用于需求。这是代码:

var urlAsset = AVURLAsset(URL: NSURL(string:self.url[indexPath.row])!, options: nil)
if(urlAsset.tracksWithMediaType(AVMediaTypeVideo).count > 0){
var imageGenerator = AVAssetImageGenerator(asset: urlAsset)
imageGenerator.appliesPreferredTrackTransform=true
let durationSeconds = CMTimeGetSeconds(urlAsset.duration)
let midPoint = CMTimeMakeWithSeconds(durationSeconds/2, 1)
var error:NSError? = nil
var actualTime = CMTimeMake(0, 0)
imageGenerator.generateCGImagesAsynchronouslyForTimes( [ NSValue(CMTime:midPoint) ], completionHandler: {
(requestTime, thumbnail, actualTime, result, error) -> Void in

if result == AVAssetImageGeneratorResult.Succeeded {

dispatch_async(dispatch_get_main_queue()) {
var thumbnailImage = UIImage(CGImage: thumbnail)
cell.imgView?.image = thumbnailImage
}//dispatch
}//if
else {
var thumbnailImage = UIImage(named: "video-backup.png")
cell.imageView?.contentMode = .ScaleAspectFit
cell.imgView?.image = thumbnailImage
}
})

关于ios - 使用 MPMoviePlayer 请求视频缩略图的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com