Skip to content

Commit

Permalink
Add autplay blocked API
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenTiigi committed Aug 6, 2024
1 parent adc9534 commit 3ee5766
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Sources/API/YouTubePlayer+EventAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ public extension YouTubePlayer {
.eraseToAnyPublisher()
}

/// A Publisher that emits whenever autoplay or scripted video playback features were blocked.
var autoplayBlockedPublisher: AnyPublisher<Void, Never> {
self.autoplayBlockedSubject
.eraseToAnyPublisher()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extension YouTubePlayer.JavaScriptEvent {
case onPlaybackRateChange
/// Error
case onError
/// Autoplay blocked
case onAutoplayBlocked
}

}
18 changes: 12 additions & 6 deletions Sources/Resources/YouTubePlayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,41 @@
window.location.href = locationHref
}

// YouTube Player onReady event callback
function onReady(event) {
// YouTube Player onReady event handler
function onReady() {
// Send onReady event
sendYouTubePlayerEvent('onReady');
}

// YouTube Player onStateChange event callback
// YouTube Player onStateChange event handler
function onStateChange(event) {
// Send onStateChange event
sendYouTubePlayerEvent('onStateChange', event.data);
}

// YouTube Player onPlaybackQualityChange event callback
// YouTube Player onPlaybackQualityChange event handler
function onPlaybackQualityChange(event) {
// Send onPlaybackQualityChange event
sendYouTubePlayerEvent('onPlaybackQualityChange', event.data);
}

// YouTube Player onPlaybackRateChange event callback
// YouTube Player onPlaybackRateChange event handler
function onPlaybackRateChange(event) {
// Send onPlaybackRateChange event
sendYouTubePlayerEvent('onPlaybackRateChange', event.data);
}

// YouTube Player onError event callback
// YouTube Player onError event handler
function onError(event) {
// Send onError error
sendYouTubePlayerEvent('onError', event.data);
}

// YouTube Player onAutoplayBlocked event handler
function onAutoplayBlocked() {
// Send onAutoplayBlocked event
sendYouTubePlayerEvent('onAutoplayBlocked');
}
</script>
</body>

Expand Down
6 changes: 6 additions & 0 deletions Sources/YouTubePlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public final class YouTubePlayer: ObservableObject {
/// The YouTubePlayer PlaybackRate CurrentValueSubject
private(set) lazy var playbackRateSubject = CurrentValueSubject<PlaybackRate?, Never>(nil)

/// The YouTubePlayer auto play blocked PassthroughSubject
private(set) lazy var autoplayBlockedSubject = PassthroughSubject<Void, Never>()

/// The YouTubePlayer WebView
private(set) lazy var webView: YouTubePlayerWebView = {
// Initialize a YouTubePlayerWebView
Expand Down Expand Up @@ -264,6 +267,9 @@ private extension YouTubePlayer {
.flatMap(YouTubePlayer.Error.init)
.map { .error($0) }
.map { self.playerStateSubject.send($0) }
case .onAutoplayBlocked:
// Send auto play blocked event
self.autoplayBlockedSubject.send(())
}
}

Expand Down

0 comments on commit 3ee5766

Please sign in to comment.