【问题标题】:Issue with requesting the video thumbnail using MPMoviePlayer使用 MPMoviePlayer 请求视频缩略图的问题
【发布时间】:2015-02-23 05:47:22
【问题描述】:

我正在尝试解析包含视频网址和标题的 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
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    实现这一点的最佳方法是使用 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
                    }
                })
    

    【讨论】:

    • 我正在尝试使用 generateCGImagesAsynchronouslyForTimes 方法,但它需要永远并冻结 UI,我不明白为什么。你能解释一下吗。我在这里发布了我的问题stackoverflow.com/questions/37394920/…
    猜你喜欢
    • 2015-11-18
    • 2010-12-01
    • 2023-04-05
    • 2010-12-27
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2013-06-15
    相关资源
    最近更新 更多