3

When using the YouTube helper library for iOS, I want to be able to detect the change of state when a user exits the player to stop playback. I've tried to use kYTPlayerStateEnded but that only triggers after the video has reached the end.

After looking through the YTPlayerView.m file, I can't seem to find a state that will achieve this. Is there another means of detecting when you've pressed 'Done' and exited the player view?

JAL
  • 41,701
  • 23
  • 172
  • 300

2 Answers2

6

What do you mean "exits the player?" Do you mean when the user pauses this video or if the user exits the full screen player?

For leaving full the screen player, you can use the UIWindowDidBecomeVisibleNotification notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoExitFullScreen:) name:UIWindowDidBecomeVisibleNotification object:self.view.window];

For detecting pauses, you can listen for the YTPlayerState kYTPlayerStatePaused in the state change callback:

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

If your YTPlayerView is a subview of a UIViewController, you can use viewWillDisappear: on the view controller to clean up your player.

JAL
  • 41,701
  • 23
  • 172
  • 300
2

To get around the issue you are describing you can use a notification with UIWindow.didBecomeHiddenNotification (Swift). This should be placed in the view controller where you are playing the video.

NotificationCenter.default.addObserver(self, selector: #selector(playerExited), name: UIWindow.didBecomeHiddenNotification, object: nil)
jsip
  • 49
  • 9