播放YouTube播放器的活动

时间:2022-05-08 19:00:08

I use Xcode Swift 7 and 2. I want to know how to listen to the YouTube player events. I am using the YouTube API. I would like to know more precisely when is it that my player is finished loading.

我使用Xcode Swift 7和2.我想知道如何收听YouTube播放器事件。我正在使用YouTube API。我想更准确地知道我的播放器何时完成加载。

1 个解决方案

#1


0  

I'm using Swift 2.1, Xcode 7.1, and Youtube API v3.

我正在使用Swift 2.1,Xcode 7.1和Youtube API v3。

I found this tutorial quite helpful: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

我发现本教程非常有用:https://developers.google.com/youtube/v3/guides/ios_youtube_helper

If you're using cocoapods it should be even easier but I just manually copied over the .h and .m and created a Bridging-Header.

如果你正在使用cocoapods它应该更容易,但我只是手动复制.h和.m并创建了一个Bridging-Header。

To setup the delegate is quite easy. First you have to tell your VC to conform to the YTPlayerViewDelegate like so:

设置委托非常简单。首先,您必须告诉您的VC符合YTPlayerViewDelegate,如下所示:

播放YouTube播放器的活动

Then make an outlet for your YTPlayerView and call [playerViewNameHere].delegate = self in your viewDidLoad.

然后为YTPlayerView创建一个插座,并在viewDidLoad中调用[playerViewNameHere] .delegate = self。

You can implement didChangeToSate like this:

您可以像这样实现didChangeToSate:

// MARK: YTPlayerDelegate methods

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) {
    switch(state) {
    case YTPlayerState.Unstarted:
        print("Unstarted")
        break
    case YTPlayerState.Queued:
        print("Ready to play")
        break
    case YTPlayerState.Playing:
        print("Video playing")
        break
    case YTPlayerState.Paused:
        print("Video paused")
        break
    default:
        break
    }

} 

I only get the play and pause notifications when the fullscreen view opens though.

我只在全屏视图打开时才会收到播放和暂停通知。

Hope this helps!

希望这可以帮助!

#1


0  

I'm using Swift 2.1, Xcode 7.1, and Youtube API v3.

我正在使用Swift 2.1,Xcode 7.1和Youtube API v3。

I found this tutorial quite helpful: https://developers.google.com/youtube/v3/guides/ios_youtube_helper

我发现本教程非常有用:https://developers.google.com/youtube/v3/guides/ios_youtube_helper

If you're using cocoapods it should be even easier but I just manually copied over the .h and .m and created a Bridging-Header.

如果你正在使用cocoapods它应该更容易,但我只是手动复制.h和.m并创建了一个Bridging-Header。

To setup the delegate is quite easy. First you have to tell your VC to conform to the YTPlayerViewDelegate like so:

设置委托非常简单。首先,您必须告诉您的VC符合YTPlayerViewDelegate,如下所示:

播放YouTube播放器的活动

Then make an outlet for your YTPlayerView and call [playerViewNameHere].delegate = self in your viewDidLoad.

然后为YTPlayerView创建一个插座,并在viewDidLoad中调用[playerViewNameHere] .delegate = self。

You can implement didChangeToSate like this:

您可以像这样实现didChangeToSate:

// MARK: YTPlayerDelegate methods

func playerView(playerView: YTPlayerView!, didChangeToState state: YTPlayerState) {
    switch(state) {
    case YTPlayerState.Unstarted:
        print("Unstarted")
        break
    case YTPlayerState.Queued:
        print("Ready to play")
        break
    case YTPlayerState.Playing:
        print("Video playing")
        break
    case YTPlayerState.Paused:
        print("Video paused")
        break
    default:
        break
    }

} 

I only get the play and pause notifications when the fullscreen view opens though.

我只在全屏视图打开时才会收到播放和暂停通知。

Hope this helps!

希望这可以帮助!