Renat Gatin
Renat Gatin

Reputation: 6394

How to hide controls in YTPlayerView?

I am using youtube-ios-player-helper library to play youtube videos in my app. There is a small time delay between you press play button and video starts playing in the full screen. So I want to hide controls in this between time.

How can I do this?

Here what I have right now in 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
  }
}

Upvotes: 3

Views: 2658

Answers (1)

Renat Gatin
Renat Gatin

Reputation: 6394

Oh, I have just found the solution: I need to set the "controls" to (0) false value in playerVars; So in my context it will look like this: let playerVars:[NSObject:AnyObject] = [ "controls":0, ... ]

And it just worked!

Upvotes: 6

Related Questions