Skip to content

Instantly share code, notes, and snippets.

@fabirt
Forked from Pobe16/YouTubeView.swift
Created April 3, 2021 19:52
Show Gist options
  • Select an option

  • Save fabirt/744a1f38265ec4ab463539d26b7218dd to your computer and use it in GitHub Desktop.

Select an option

Save fabirt/744a1f38265ec4ab463539d26b7218dd to your computer and use it in GitHub Desktop.

Revisions

  1. @Pobe16 Pobe16 revised this gist Jun 12, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion YouTubeView.swift
    Original file line number Diff line number Diff line change
    @@ -91,8 +91,11 @@ final class YouTubeView: UIViewRepresentable {
    switch playerState {
    case .Playing:
    self.playerState.videoState = .play
    case .Paused, .Ended, .Buffering, .Unstarted:
    case .Paused, .Buffering, .Unstarted:
    self.playerState.videoState = .pause
    case .Ended:
    self.playerState.videoState = .stop
    self.playerState.videoID = loadNextVideo()
    default:
    print("\(playerState) not implemented")
    }
  2. @Pobe16 Pobe16 revised this gist Jun 12, 2020. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions YouTubeView.swift
    Original file line number Diff line number Diff line change
    @@ -96,9 +96,6 @@ final class YouTubeView: UIViewRepresentable {
    default:
    print("\(playerState) not implemented")
    }


    }
    }

    }
  3. @Pobe16 Pobe16 created this gist Jun 12, 2020.
    104 changes: 104 additions & 0 deletions YouTubeView.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    import SwiftUI
    import UIKit
    import YouTubePlayer

    final class YouTubeView: UIViewRepresentable {

    typealias UIViewType = YouTubePlayerView

    @ObservedObject var playerState: YouTubeControlState

    init(playerState: YouTubeControlState) {
    self.playerState = playerState
    }

    func makeCoordinator() -> Coordinator {
    Coordinator(playerState: playerState)
    }

    func makeUIView(context: Context) -> UIViewType {
    let playerVars = [
    "controls": "1",
    "playsinline": "0",
    "autohide": "0",
    "autoplay": "0",
    "fs": "1",
    "rel": "0",
    "loop": "0",
    "enablejsapi": "1",
    "modestbranding": "1"
    ]

    let ytVideo = YouTubePlayerView()

    ytVideo.playerVars = playerVars as YouTubePlayerView.YouTubePlayerParameters
    ytVideo.delegate = context.coordinator

    return ytVideo
    }

    func updateUIView(_ uiView: UIViewType, context: Context) {

    guard let videoID = playerState.videoID else { return }

    if !(playerState.executeCommand == .idle) && uiView.ready {
    switch playerState.executeCommand {
    case .loadNewVideo:
    playerState.executeCommand = .idle
    uiView.loadVideoID(videoID)
    case .play:
    playerState.executeCommand = .idle
    uiView.play()
    case .pause:
    playerState.executeCommand = .idle
    uiView.pause()
    case .forward:
    playerState.executeCommand = .idle
    uiView.getCurrentTime { (time) in
    guard let time = time else {return}
    uiView.seekTo(Float(time) + 10, seekAhead: true)
    }
    case .backward:
    playerState.executeCommand = .idle
    uiView.getCurrentTime { (time) in
    guard let time = time else {return}
    uiView.seekTo(Float(time) - 10, seekAhead: true)
    }
    default:
    playerState.executeCommand = .idle
    print("\(playerState.executeCommand) not yet implemented")
    }
    } else if !uiView.ready {
    uiView.loadVideoID(videoID)
    }

    }

    class Coordinator: YouTubePlayerDelegate {
    @ObservedObject var playerState: YouTubeControlState

    init(playerState: YouTubeControlState) {
    self.playerState = playerState
    }

    func playerReady(_ videoPlayer: YouTubePlayerView) {
    videoPlayer.play()
    playerState.videoState = .play
    }

    func playerStateChanged(_ videoPlayer: YouTubePlayerView, playerState: YouTubePlayerState) {

    switch playerState {
    case .Playing:
    self.playerState.videoState = .play
    case .Paused, .Ended, .Buffering, .Unstarted:
    self.playerState.videoState = .pause
    default:
    print("\(playerState) not implemented")
    }


    }
    }

    }