Skip to content

Instantly share code, notes, and snippets.

@Pobe16
Last active June 12, 2020 12:58
Show Gist options
  • Save Pobe16/c7d91d61a6de2cb1f48f03955448fcc8 to your computer and use it in GitHub Desktop.
Save Pobe16/c7d91d61a6de2cb1f48f03955448fcc8 to your computer and use it in GitHub Desktop.
Controller Object for YouTube Video
// 1
enum playerCommandToExecute {
case loadNewVideo
case play
case pause
case forward
case backward
case stop
case idle
}
// 2
class YouTubeControlState: ObservableObject {
// 3
@Published var videoID: String? // = "qRC4Vk6kisY"
{
// 4
didSet {
self.executeCommand = .loadNewVideo
}
}
// 5
@Published var videoState: playerCommandToExecute = .loadNewVideo
// 6
@Published var executeCommand: playerCommandToExecute = .idle
// 7
func playPauseButtonTapped() {
if videoState == .play {
pauseVideo()
} else if videoState == .pause {
playVideo()
} else {
print("Unknown player state, attempting playing")
playVideo()
}
}
// 8
func playVideo() {
executeCommand = .play
}
func pauseVideo() {
executeCommand = .pause
}
func forward() {
executeCommand = .forward
}
func backward() {
executeCommand = .backward
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment