I have Youtube embed code using iframe
<iframe title="YouTube" src=";amp;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!
I have Youtube embed code using iframe
<iframe title="YouTube" src="http://www.youtube./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!
Share Improve this question asked Oct 13, 2016 at 12:50 tophertopher 1,3974 gold badges17 silver badges41 bronze badges 1- 1 Have you looked at the youtube iframe api docs? – evolutionxbox Commented Oct 13, 2016 at 12:51
1 Answer
Reset to default 5It'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