diff --git a/Sources/API/YouTubePlayer+EventAPI.swift b/Sources/API/YouTubePlayer+EventAPI.swift index 5acc2bf..f82932c 100644 --- a/Sources/API/YouTubePlayer+EventAPI.swift +++ b/Sources/API/YouTubePlayer+EventAPI.swift @@ -77,4 +77,10 @@ public extension YouTubePlayer { .eraseToAnyPublisher() } + /// A Publisher that emits whenever autoplay or scripted video playback features were blocked. + var autoplayBlockedPublisher: AnyPublisher { + self.autoplayBlockedSubject + .eraseToAnyPublisher() + } + } diff --git a/Sources/JavaScriptEvent/YouTubePlayer+JavaScriptEvent+Name.swift b/Sources/JavaScriptEvent/YouTubePlayer+JavaScriptEvent+Name.swift index 7bb0e2a..dfbee6f 100644 --- a/Sources/JavaScriptEvent/YouTubePlayer+JavaScriptEvent+Name.swift +++ b/Sources/JavaScriptEvent/YouTubePlayer+JavaScriptEvent+Name.swift @@ -20,6 +20,8 @@ extension YouTubePlayer.JavaScriptEvent { case onPlaybackRateChange /// Error case onError + /// Autoplay blocked + case onAutoplayBlocked } } diff --git a/Sources/Resources/YouTubePlayer.html b/Sources/Resources/YouTubePlayer.html index f6b2ac0..731f6e5 100644 --- a/Sources/Resources/YouTubePlayer.html +++ b/Sources/Resources/YouTubePlayer.html @@ -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'); + } diff --git a/Sources/YouTubePlayer.swift b/Sources/YouTubePlayer.swift index 4227e23..7a2d6c7 100644 --- a/Sources/YouTubePlayer.swift +++ b/Sources/YouTubePlayer.swift @@ -53,6 +53,9 @@ public final class YouTubePlayer: ObservableObject { /// The YouTubePlayer PlaybackRate CurrentValueSubject private(set) lazy var playbackRateSubject = CurrentValueSubject(nil) + /// The YouTubePlayer auto play blocked PassthroughSubject + private(set) lazy var autoplayBlockedSubject = PassthroughSubject() + /// The YouTubePlayer WebView private(set) lazy var webView: YouTubePlayerWebView = { // Initialize a YouTubePlayerWebView @@ -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(()) } }