如何在YTPlayerView中隐藏控件?

时间:2015-09-24 19:45:43

标签: swift youtube controls

我正在使用youtube-ios-player-helper库在我的应用中播放YouTube视频。 按下播放按钮和视频开始在全屏播放之间会有一个小的时间延迟。所以我想在这段时间内隐藏控件。

我该怎么做?

这就是我现在在Swift中所拥有的:

protocol YouTubePlayerViewDelegate {
  func playVideo(id: String)
}

class DeferredYouTubePlayerView : UIView, EventActionable, YTPlayerViewDelegate {
  var youtubeId: String!
  var origin: String!
  var playerWidth: Int!
  var imageView: ImageLoadingBackgroundView?
  var playImageView: UIImageView?
  var player: YTPlayerView?
  var backDropView: UIView?

  var eventCategory:String!
  var touchCancelled = false

  let playerVars:[NSObject:AnyObject] = [
    "playsinline":0,
    "autoplay" : 0,
    "modestbranding" : 1,
    "origin":MetroManager.sharedInstance.selectedMetro!.baseURL
  ]

  var delegate : YouTubePlayerViewDelegate?

  func loadVideo() {
    backgroundColor = UIColor.blackColor()

    if imageView == nil {
      imageView = ImageLoadingBackgroundView()
      imageView!.setTranslatesAutoresizingMaskIntoConstraints(false)
      imageView?.constrain(.FillParent(self))
    }

    if playImageView == nil {
      playImageView = UIImageView(image: UIImage(named: "play_button"))
      playImageView?.constrain(.Height(42))
      playImageView?.constrain(.Height(42))
      playImageView?.constrain(.CenterInParent(imageView!))
    }

    if let url = buildImageURL() {
      imageView?.alpha = 1
      imageView!.imageURL = url
    } else {
      imageView?.alpha = 0
    }

    if backDropView == nil {
      backDropView = UIView()
      backDropView?.backgroundColor = UIColor.blackColor()
      backDropView?.constrain(.CenterInParent(self))
      backDropView?.constrain(.FillParent(self))
      addSubview(backDropView!)
    }

    if player == nil {
      player = YTPlayerView()
      player?.constrain(.CenterInParent(self))
      player?.constrain(.FillParent(self))
      player?.delegate = self
      player!.loadWithVideoId(youtubeId, playerVars: playerVars)
      addSubview(player!)

      bringSubviewToFront(imageView!)
    }
  }

  func buildImageURL() -> NSURL? {
    return NSURL(string: "http://img.youtube.com/vi/\(youtubeId)/0.jpg")
  }

  override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let playImageView = playImageView {
      playImageView.image = UIImage(named: "play_button_highlighted")
    }
  }

  //If the user moves their finger, they probably want to slide to the next view, cancel touches
  override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let playImageView = playImageView {
      playImageView.image = UIImage(named: "play_button")
    }

    touchCancelled = true
  }

  func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) {
    switch state {
    case .Buffering:
      fallthrough
    case .Playing:
      hidePoster()
    case .Ended:
      fallthrough
    case .Paused:
      showPoster()
    default:
      ()
    }
  }

  func hidePoster() {
    UIView.animateWithDuration(DEFAULT_ANIMATION_TIME, animations: { () -> Void in
      self.imageView?.alpha = 0
    })
  }

  func showPoster() {
    UIView.animateWithDuration(DEFAULT_ANIMATION_TIME, animations: { () -> Void in
      self.imageView?.alpha = 1
    })
  }

  override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    println("ended")

    if let playImageView = playImageView {
      playImageView.image = UIImage(named: "play_button")
    }

    if(!touchCancelled){
      loggerA(category: eventCategory, action: "Cover Scroll", label: "Video Play")
//      delegate?.playVideo(youtubeId)
      hidePoster()
      player!.playVideo()

    }

    touchCancelled = false
  }

  deinit{
    //delegate = nil
  }
}

1 个答案:

答案 0 :(得分:4)

哦,我刚刚找到解决方案:我需要设置&#34;控件&#34; to(0)playerVars中的false值;所以在我的上下文中它看起来像这样:让playerVars:[NSObject:AnyObject] = [&#34; controls&#34;:0,...]

它刚刚起作用!