CMTimeGetSeconds崩溃应用程序

时间:2015-08-11 06:23:40

标签: objective-c swift ios8

我使用 AVPlayer 播放视频链接并添加观察者以更新进度条。但随着时间超过10秒的视频,我的应用程序崩溃了。以下是我的代码

  • 播放视频按钮:

    @IBAction func playVideoButtonPressed(sender: AnyObject) {
        if let contentUrl = curVideoModel?.video_Content_Url{
            player = AVPlayer(URL: NSURL(string: contentUrl))
            player?.actionAtItemEnd = AVPlayerActionAtItemEnd.None
            playerLayer = AVPlayerLayer(player: player)
            playerLayer!.frame = CGRectMake(0, 0, self.imvVideoThumbnail.frame.size.width, self.imvVideoThumbnail.frame.size.height)
            playerLayer!.backgroundColor = UIColor.blackColor().CGColor
            playerLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
            self.playvideoHolderView.layer.addSublayer(playerLayer)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "itemDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: player?.currentItem)
            player?.addPeriodicTimeObserverForInterval(CMTimeMakeWithSeconds(1.0/60.0, Int32(NSEC_PER_SEC)), queue: nil, usingBlock: { (time) -> Void in
                self.updateProgressbar()
            })
            player!.play()
        }
    }
    
  • 视频播放完毕后回调函数:

    NSNotificationCenter.defaultCenter().removeObserver(self, name: AVPlayerItemDidPlayToEndTimeNotification, object: player?.currentItem)
    self.progressBar.progress = 0
    if notification.name == AVPlayerItemDidPlayToEndTimeNotification{
        if player != nil{
            player?.pause()
        }
        if playerLayer != nil{
            playerLayer!.removeFromSuperlayer()
        }
    }
    
  • 更新进度条的功能:

    var duration: Double
    var time: Double
    if player != nil{
         duration = CMTimeGetSeconds(player!.currentItem.duration)
         time = CMTimeGetSeconds(player!.currentTime())
    } else{
        duration = 0
        time = 0
    }
    self.lblTotalTime.text = "\(Int(duration))"
    self.lblCurrentTime.text = "\(Int(time))"
    self.progressBar.progress = Float(time / duration)
    

enter image description here P / S:它在真实设备上崩溃(iPod touch iOS 8.3)。但是在模拟器上玩OK

编辑:也许Peter的评论是正确的。我检查:如果玩家!.currentItem.duration.value> 0 之前获取:持续时间= CMTimeGetSeconds(播放器!.currentItem.duration)。然后崩溃得到修复**

0 个答案:

没有答案