I want to auto click the youtube play button on page load in mobile device. I am using the iframe to show youtube video . Its auto play on desktop but showing play button on mobile device . I am using this
<script type="text/javascript">
$(function() {
$('.ytp-large-play-button').click();
});
</script>
but not working for me . Any help?
Thanks in advance
I want to auto click the youtube play button on page load in mobile device. I am using the iframe to show youtube video . Its auto play on desktop but showing play button on mobile device . I am using this
<script type="text/javascript">
$(function() {
$('.ytp-large-play-button').click();
});
</script>
but not working for me . Any help?
Thanks in advance
Share Improve this question asked Oct 30, 2017 at 6:27 Avinash GuptaAvinash Gupta 3284 silver badges15 bronze badges 7-
2
Why not use
?autoplay=1
in the embed instead? – Saurav Rastogi Commented Oct 30, 2017 at 6:28 - i want autoplay on mobile device also – Avinash Gupta Commented Oct 30, 2017 at 6:30
- i used there but only work on desktop – Avinash Gupta Commented Oct 30, 2017 at 6:30
- Have you embedded the youtube player? – Saurav Rastogi Commented Oct 30, 2017 at 6:30
- where are you testing ? I mean andriod ? IOS ? – Muhammad Usman Commented Oct 30, 2017 at 6:31
3 Answers
Reset to default 2you can use iframe onload event like this
<iframe src="http://..." onload='playVideo();'>
<script type="text/javascript">
function playVideo(){
$('.ytp-large-play-button').click();
}
</script>
Try this this function call when iframe is load
If You are using jquery make sure you have a Jquery file or You can use CDN check below.
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
use this one when your page is loaded the button is trigger.
$(document).ready(function(){
$('.ytp-large-play-button').trigger('click');
});
Videos can autoplay on desktop but why can you not get them to autoplay on mobile websites when the page loads? The answer is that the different OS developers intentionally disabled autoplay on mobile devices in order to protect user’s bandwidth. Many data providers charge based on the amount of data consumed, so the OS developers decided it was in the best interest of the user to not have a video automatically begin playing when the page loaded so it would not start racking up data charges. Instead mobile web videos require the user to click them to start.
check more
I am not sure if you can trigger event inside iFrame...
Why doesn't video Autoplay on Mobile device https://www.aerserv./why-does-video-autoplay-on-mobile-devices-not-work/
For your case try this
A solution I've just been working on for autoplaying would be to instantiate the player with an empty video on page load:
var player = new YT.Player('yt-media-player', {
height: height,
width: width,
videoId: ''
});
Then on the click event of opening the lightbox you can load the particular video and call it to play:
player.loadVideoById(youtubeId);
player.playVideo();
Details here