2

I'm having an issue trying to autoplay my UIWebView.

Yes, I know there are tons of questions regarding it already.

Fist off, here's my simple code:

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let youtubePlayer:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))

    youtubePlayer.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.youtube.com/embed/EzNinU0g-Q0")!) )

    self.view.addSubview(youtubePlayer)
}

Now here's what I found:

  1. How to autoplay a YouTube video in a UIWebView
  2. Youtube video autoplay inside UIWebview
  3. How to get YouTube autoplay to work in UIWebView?

I did as suggested with the following codes:

youtubePlayer.mediaPlaybackRequiresUserAction = false
youtubePlayer.allowsInlineMediaPlayback = true

That did nothing.

  1. autoplay on video in UIWebview,iOS

This solution works, however, it's not for embedded videos, which is what I'm using.

Therefore, is there no way to auto-play using the UIWebView?

Those questions were posted years ago, so I'm not sure how things have changed.

Can anyone suggest an alternative? Since I have a UITableView set up to where, when the user selects a cell (video), it will show the UIWebView of the video.

However, I would like, if possible for the video to start playing immediately when the user selects a video instead of having to press play again to play it.

I have a similar layout design to apps like TubiMusic and Tubex, but those apps are able to autoplay videos while I can't figure out how to do the same.

Any help would be appreciated. Thanks

Community
  • 1
  • 1
Pangu
  • 3,721
  • 11
  • 53
  • 120

2 Answers2

4

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)

}
Community
  • 1
  • 1
owlswipe
  • 19,159
  • 9
  • 37
  • 82
2

Autoplay parameter does not work on web view. Instead you have to use the JS apis to determine the readiness state of the player and then send a play command to that.

You can use this pod https://github.com/gilesvangruisen/Swift-YouTube-Player

or you can use the google provided code

https://developers.google.com/youtube/v3/guides/ios_youtube_helper

Pradeep K
  • 3,671
  • 1
  • 11
  • 15