Skip to content

Instantly share code, notes, and snippets.

@Pobe16
Created June 12, 2020 15:55
Show Gist options
  • Save Pobe16/90d538f8eaa905c409f5246b5e404e82 to your computer and use it in GitHub Desktop.
Save Pobe16/90d538f8eaa905c409f5246b5e404e82 to your computer and use it in GitHub Desktop.
ytviewrepresentable coordinator
// 1
class Coordinator: YouTubePlayerDelegate {
// 2
@ObservedObject var playerState: YouTubeControlState
init(playerState: YouTubeControlState) {
self.playerState = playerState
}
// 3
func playerReady(_ videoPlayer: YouTubePlayerView) {
videoPlayer.play()
playerState.videoState = .play
}
// 4
func playerStateChanged(_ videoPlayer: YouTubePlayerView, playerState: YouTubePlayerState) {
switch playerState {
case .Playing:
self.playerState.videoState = .play
case .Paused, .Buffering, .Unstarted:
self.playerState.videoState = .pause
case .Ended:
self.playerState.videoState = .stop
// 5
self.playerState.videoID = loadNextVideo()
default:
print("\(playerState) not implemented")
}
}
}
// 6
func makeCoordinator() -> Coordinator {
Coordinator(playerState: playerState)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment