Reputation: 1397
I have Youtube embed code using iframe
<iframe title="YouTube" src="http://www.youtube.com/embed/aaabbbcccddd?wmode=transparent&autoplay=0" allowfullscreen="allowfullscreen"></iframe>
I want that if users click the "Play" button, it automatically goes fullscreen.
As opposed to the user should click "Play" and click "Fullscreen" button -- I want to skip that click "Fullscreen" part.
Is this possible?
Do I need to use Youtube's Javascript API?
Does it have something like this:
youtube.onPlayButtonClicked(function() {
youtube.fullscreen();
});
Thank you in advance!
Upvotes: 5
Views: 7780
Reputation: 481
It's possible with the youtube API.
var player, iframe;
var $ = document.querySelector.bind(document);
// init player
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '200',
width: '300',
videoId: 'dQw4w9WgXcQ',
events: {
'onReady': onPlayerReady
}
});
}
// when ready, wait for clicks
function onPlayerReady(event) {
var player = event.target;
iframe = $('#player');
setupListener();
}
function setupListener (){
$('button').addEventListener('click', playFullscreen);
}
function playFullscreen (){
player.playVideo();//won't work on mobile
var requestFullScreen = iframe.requestFullScreen || iframe.mozRequestFullScreen || iframe.webkitRequestFullScreen;
if (requestFullScreen) {
requestFullScreen.bind(iframe)();
}
}
You can have an exemple at this link : https://codepen.io/bfred-it/pen/GgOvLM
Upvotes: 5