Youtube Embedded Video: Autoplay Feature Not Working in Iphone

Youtube embedded video: autoplay feature not working in iphone

It can't be done. For various reasons (including, but not limited to data usage), Apple doesn't allow auto-playing of videos.

See the accepted answer to this question.

Youtube autoplay not working on mobile devices with embedded HTML5 player

As it turns out, autoplay cannot be done on iOS devices (iPhone, iPad, iPod touch) and Android.

See https://stackoverflow.com/a/8142187/2054512 and https://stackoverflow.com/a/3056220/2054512

Is it not possible to autoplay embedded YouTube videos using UIWebView?

Here is a Swift-ified version of the top-rated obj-c answer to How to autoplay a YouTube video in a UIWebView.

Apparently the problem was with the nil base url, when I changed it to resourceURL the autoplay worked.

Here's the code:

var youTubeVideoHTML: String = "<!DOCTYPE html><html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = \"http://www.youtube.com/player_api\"; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%0.0f', height:'%0.0f', videoId:'%@', events: { 'onReady': onPlayerReady, } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>"

func playVideoWithId(videoId: String) {
var html: String = String(format: youTubeVideoHTML, self.frame.size.width, self.frame.size.height, videoId)

}


Related Topics



Leave a reply



Submit