2015-02-12 26 views
1

我想解析具有視頻網址和標題的JSON數據。解析數據後,我在UITableviewCell中顯示視頻縮略圖。問題是,當數據被解析並且它的時間要求縮略圖時,NSNotification選擇器方法從未被調用過。這裏是我的代碼:問題使用MPMoviePlayer請求視頻縮略圖

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 
} 

回答

0

實現這一目標的最好方法是使用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 
       } 
      }) 
+0

我想使用generateCGImagesAsynchronouslyForTimes方法,但它需要永久凍結的用戶界面,我不明白爲什麼。你能解釋一下嗎?我已經在這裏發佈我的問題http://stackoverflow.com/questions/37394920/ios-swift-generatecgimagesasynchronouslyfortimes-takes-too-long-and-freezes-th – Sam 2016-05-23 19:54:48