Skip to content

Commit

Permalink
Improve event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenTiigi committed Feb 2, 2025
1 parent 97d1e52 commit b296749
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions Sources/YouTubePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,6 @@ private extension YouTubePlayer {
case .ready:
// Send ready state
self.stateSubject.send(.ready)
// Check if autoPlay is enabled
if self.parameters.autoPlay == true && self.source != nil && !self.isPlaying {
// Play Video
Task(priority: .userInitiated) { [weak self] in
try? await self?.play()
}
}
// Initially load playback state
Task { [weak self] in
guard let playbackState = try? await self?.getPlaybackState() else {
return
}
self?.playbackStateSubject.send(playbackState)
}
case .stateChange:
// Verify YouTubePlayer PlaybackState is available
guard let playbackState = playerEvent
Expand All @@ -301,10 +287,11 @@ private extension YouTubePlayer {
// Otherwise return out of function
return
}
// Check if state is set to error
// Check if playback state is not equal to unstarted which mostly is an error
// and the state is currently set to error
if playbackState != .unstarted, case .error = self.state {
// Handle onReady state
self.handle(playerEvent: .init(name: .ready))
// Send ready state as the player has recovered from an error
self.stateSubject.send(.ready)
}
// Send PlaybackState
self.playbackStateSubject.send(playbackState)
Expand Down

0 comments on commit b296749

Please sign in to comment.