检查有效网址在avplayercontroller中播放?

时间:2016-09-02 09:22:18

标签: ios swift avplayer avplayerviewcontroller

我有三个网址,必须检查这些网址是否有视频链接可以播放。如果它不包含网址中的视频并从下一个网址播放视频,我必须esc链接。以下代码将有助于跟踪播放状态。

let playerAV = AVPlayerViewController()
  var player = AVPlayer()
  playerAV.player = player
  playerAV.view.frame = CGRectMake(0, 0,  self.videoView.frame.width,  self.videoView.frame.height)
  self.addChildViewController(playerAV)
  self.videoView.addSubview(playerAV.view)
  playerAV.didMoveToParentViewController(self)
  playerAV.player?.play()
  addObserverOfMoviePlayer()

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
    _ = UIDevice.beginGeneratingDeviceOrientationNotifications
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.deviceOrientationDidChange(_:)) , name:
      UIDeviceOrientationDidChangeNotification, object: nil)
    playerAV.player!.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
    playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackBufferEmpty", options: NSKeyValueObservingOptions.New, context: nil)
    playerAV.player!.currentItem!.addObserver(self, forKeyPath: "playbackLikelyToKeepUp", options: NSKeyValueObservingOptions.New, context: nil)

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

    if keyPath == "rate"   {

      if let rate = change?[NSKeyValueChangeNewKey] as? Float {
        if playerAV.player!.currentItem!.status == AVPlayerItemStatus.ReadyToPlay{
          if rate != 0 && playerAV.player!.error == nil {

            print("normal playback")
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ChannelDetailViewController.somAction), userInfo: nil, repeats: true)

          }
          else{
            timer?.invalidate()
            print("movie player stopped")
          }

        }else if playerAV.player!.currentItem?.status == AVPlayerItemStatus.Unknown{
          timer?.invalidate()
          print("not ready to play")
        }
      }
    }
    if keyPath == "playbackBufferEmpty"{

      if playerAV.player?.currentItem?.playbackBufferEmpty == true{
        timer?.invalidate()
        print("movie player stopped due to no buffer")
      }
    }

    if keyPath == "playbackLikelyToKeepUp" {

      if playerAV.player?.currentItem?.playbackLikelyToKeepUp == true{

        print("movie player playing after enough data in buffer")
        playerAV.player?.play()

      }
    }
  }

1 个答案:

答案 0 :(得分:6)

只需检查视频的长度,如果该视频无效,则此代码的长度将返回为零:

let assets = AVAsset(URL: NSURL(fileURLWithPath: filePath))
let length=Float(assets.duration.value)/Float(assets.duration.timescale)
if length != 0.0  {
// play it
}

如果您还有问题,可以随时问我。