diff --git a/README.md b/README.md index 56211ab..ccbbef2 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,16 @@ By default most embedded services are shown at a ratio of `16:9`. Some services The aspect ratio is maintained at different viewport sizes. +### Setting AutoPlay + +By default, this setting is set to `false`. If you want to set the autoplay to true, you can do so by passing in a boolean to the `auto-play` attribute. + +This feature is only supported by YouTube and Vimeo services. + +```html + +``` + ### Fallback If no service exists to handle the URL a fallback view is rendered. You can customize this by publishing the views and editing `/resources/views/vendor/embed/services/fallback.blade.php`. diff --git a/resources/views/services/vimeo.blade.php b/resources/views/services/vimeo.blade.php index d100124..b54f22a 100644 --- a/resources/views/services/vimeo.blade.php +++ b/resources/views/services/vimeo.blade.php @@ -1,7 +1,7 @@ - diff --git a/src/ServiceBase.php b/src/ServiceBase.php index 747700b..d269b7f 100644 --- a/src/ServiceBase.php +++ b/src/ServiceBase.php @@ -16,6 +16,8 @@ abstract class ServiceBase implements ServiceContract protected ?string $label; + protected ?bool $autoPlay; + public function __construct(Url $url) { $this->url = $url; @@ -52,6 +54,13 @@ public function setLabel(?string $label): ServiceContract return $this; } + public function setAutoPlay(?bool $autoPlay): ServiceContract + { + $this->autoPlay = $autoPlay ?? $this->defaultAutoPlay(); + + return $this; + } + protected function viewName(): string { return $this->guessViewName(); @@ -67,6 +76,11 @@ protected function label(): string return $this->label ?? $this->defaultLabel(); } + protected function autoPlay(): bool + { + return $this->autoPlay ?? $this->defaultAutoPlay(); + } + protected function defaultAspectRatio(): Ratio { return new Ratio('16:9'); @@ -77,6 +91,11 @@ protected function defaultLabel(): string return __('An embedded video'); } + protected function defaultAutoPlay(): bool + { + return false; + } + private function fullyQualifiedViewName(): string { return 'embed::services.' . $this->viewName(); diff --git a/src/ServiceContract.php b/src/ServiceContract.php index cc5240e..f446e7d 100644 --- a/src/ServiceContract.php +++ b/src/ServiceContract.php @@ -13,4 +13,5 @@ public function view(): View; public function cacheAndRender(): string; public function setAspectRatio(?Ratio $aspectRatio): ServiceContract; public function setLabel(?string $label): ServiceContract; + public function setAutoPlay(?bool $autoPlay): ServiceContract; } \ No newline at end of file diff --git a/src/Services/Vimeo.php b/src/Services/Vimeo.php index 6a2c86b..5bc63d6 100644 --- a/src/Services/Vimeo.php +++ b/src/Services/Vimeo.php @@ -27,7 +27,8 @@ protected function parseUrl(): ?array if (array_key_exists(5, $match)) { return [ 'videoId' => $match[5], - 'videoHash' => isset($match[6]) ? $match[6] : NULL + 'videoHash' => isset($match[6]) ? $match[6] : NULL, + 'autoplay' => $this->autoPlay() ? '?autoplay=1' : NULL, ]; } diff --git a/src/Services/YouTube.php b/src/Services/YouTube.php index 5d021eb..7ba79dd 100644 --- a/src/Services/YouTube.php +++ b/src/Services/YouTube.php @@ -16,6 +16,7 @@ protected function viewData(): array { return [ 'videoId' => $this->videoId(), + 'autoplay' => $this->autoPlay() ? '?autoplay=1&mute=1' : NULL ]; } diff --git a/src/ViewComponents/EmbedViewComponent.php b/src/ViewComponents/EmbedViewComponent.php index de31fff..682673f 100644 --- a/src/ViewComponents/EmbedViewComponent.php +++ b/src/ViewComponents/EmbedViewComponent.php @@ -15,12 +15,14 @@ class EmbedViewComponent extends Component protected Url $url; protected ?Ratio $aspectRatio; protected ?string $label; + protected ?bool $autoPlay; - public function __construct(string $url, string $aspectRatio = null, string $label = null) + public function __construct(string $url, string $aspectRatio = null, string $label = null, bool $autoPlay = false) { $this->url = new Url($url); $this->aspectRatio = $aspectRatio ? new Ratio($aspectRatio) : null; $this->label = $label; + $this->autoPlay = $autoPlay; } public function render(): string @@ -34,6 +36,7 @@ public function render(): string return $this->service ->setAspectRatio($this->aspectRatio) ->setLabel($this->label) + ->setAutoPlay($this->autoPlay) ->cacheAndRender(); } }