I copied a code from a post here and modified it a little. The autoplay works fine (with javascript) on my computers, but not on iPad. I've been trying with autoplay: 1 inside onYouTubeIframeAPIRead but it doesn't help.
<!-- 1. The <iframe> (and video player) will replace this <div> tag.-->
<div id="player">
</div>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '100%',
width: '100%',
videoId: '<?php echo $video;?>',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, <?php echo $length*1000+2000;?>);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
iOS blocks autoplay on videos that have audio tracks and are autoplayed through a handler that is not in the same direct context as a user interaction.
Relevant blog post: https://webkit.org/blog/6784/new-video-policies-for-ios/
You'll want to bind this to a handler and maintain the same overall context so that autoplay is allowed. I believe Chrome is also going to be implementing this policy very soon.