2

I'm playing You Tube videos in my app and trying to detect when a video is paused or ended? I've found the kYTPlayerState function in the .m file, but having trouble converting it's use to Swift. Here is what I have....

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState)
{
    switch (state) {
    case YTPlayerState.Playing:
        print("Started playback");
        break;
    case YTPlayerState.Paused:
        print("Paused playback");
        break;
    case YTPlayerState.Ended:
        print("Ended playback");
        break;
    default:
        break;
    }
}

and .....

   if playerView(player , didChangeToState: YTPlayerState.Ended)
    {
        /// Do Something....
    }

....which I place in ViewDidLoad(). I'm also getting back the error on my if statement that says Type "()" does not conform to protocol "Boolean Type" . If you know how to fix this, or if I'm leaving something out please help.

I'm initializing the player in this view with @IBOutlet var player: YTPlayerView!

JAL
  • 41,701
  • 23
  • 172
  • 300
Charles Jr
  • 8,333
  • 15
  • 53
  • 74
  • Hey Charles did my answer help you? Let me know if you have any other questions. – JAL Apr 05 '16 at 01:43
  • @JAL Yes, It worked great. I think I got confused and tried to implement the `if state` statement in viewWillLoad because I've done that with other events/monitoring protocols. – Charles Jr Apr 07 '16 at 22:35
  • Great, glad to hear it! Feel free to mark my answer as accepted to show other users with the same issue that my answer helped you. If you have any other questions, don't hesitate to follow up with me here or in another question. Happy coding! – JAL Apr 07 '16 at 22:43

1 Answers1

2

playerView:didChangeToState is a delegate method, a callback. You shouldn't be calling it directly like in your second code block. If you set the YTPlayerViewDelegate to your view controller (player.delegate = self in viewDidLoad) playerView:didChangeToState should be called automatically for you as player changes state. Just make sure you implement the state change method.

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState)
{
    case YTPlayerState.Ended:
        // handle ended state
        break;
    default:
        break;
    }
}
JAL
  • 41,701
  • 23
  • 172
  • 300
  • Hi @JAL, I used the same think but when I add the `YTPlayerState.unstarted` and `YTPlayerState.queued`, their notifications are not handled. Have you please any idea about that? – Ne AS Nov 11 '16 at 09:44