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
Best Way to Include Css? Why Use @Import
Responsive Separator For Horizontal List
Angular 5 Formgroup Reset Doesn't Reset Validators
What Is "X-Content-Type-Options=Nosniff"
Css ''Background-Color" Attribute Not Working on Checkbox Inside ≪Div≫
Is Type="Text/Css" Necessary in a ≪Link≫ Tag
What Are the Allowed Tags Inside a ≪Li≫
Font Awesome Icon Inside Text Input Element
Are Empty HTML5 Data Attributes Valid
Android: How to Use the HTML.Taghandler
Removing Ul Indentation with CSS
HTML Select Box Options on Hover
Do You Have to Include <Link Rel="Icon" Href="Favicon.Ico" Type="Image/X-Icon" />
Jquery: 'Uncaught Typeerror: Illegal Invocation' at Ajax Request - Several Elements
Weird Effect When Applying Transparent Border Over an Element with a Gradient Background