ylliX - Online Advertising Network

Detecting changes in YTPlayer library

Recently I noticed this may not be such obvious, but if you need to know when user pauses video or resumes playback, just use:

- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state;

and since there are constants like kYTPlayerStatePlaying or kYTPlayerStatePaused  you can just do:

- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state {
  switch (state) {
    case kYTPlayerStatePlaying:
      // do something here
    case kYTPlayerStatePaused:
      // do something else here

but to make it work, do not forget to set proper delegate:

myPlayer.delegate = self;

and add YTPlayerViewDelegate in your header file for view controller:

@interface ViewController : UIViewController <YTPlayerViewDelegate>

There are also few other useful methods:

- (void)playerViewDidBecomeReady:(YTPlayerView *)playerView;
- (void)playerView:(YTPlayerView *)playerView didChangeToQuality:(YTPlaybackQuality)quality;
- (void)playerView:(YTPlayerView *)playerView receivedError:(YTPlayerError)error;

so there is pretty easy to handle most of player behavior.

Leave a Reply