2

Platform: iOS 6.1. Xcode 4.6.

I am using YouTube Javascript API and a UIWebView. I am able to manually tap on the video thumbnail to begin playback just fine. But, autoplay is not working. I am setting mediaPlaybackRequiresUserAction = NO for the web view as many have suggested. No luck.

Here is the code:

@interface VideoPlayerController : UIViewController <UIWebViewDelegate>
    @property(strong, nonatomic) IBOutlet UIWebView* webView;
@end

- (void) viewDidLoad
{
    [super viewDidLoad];    

    self.webView.delegate = self;

    NSString *template = [NSString stringWithContentsOfFile:
                       [[NSBundle mainBundle]
                        pathForResource:@"YouTubeTemplate" ofType:@"txt"]];
    NSString *htmlStr = [NSString stringWithFormat: template,
        200, 200,
        @"3G4exdlsRkY"];
    self.webView.mediaPlaybackRequiresUserAction = NO;
    [self.webView loadHTMLString:htmlStr baseURL:nil];
}

- (void) webViewDidFinishLoad:(UIWebView *)wv {
    self.webView.mediaPlaybackRequiresUserAction = NO;
}

The YouTubeTemplate.txt file is:

<html>
<head>
<script src="https://www.youtube.com/player_api"></script>
<style>
body, div {
    margin: 0px;
    padding: 0px;
}
</style>
</head>
<body>
  <div id="media_area"></div>
</body>
<script>
var ytPlayer = null;

function onYouTubePlayerAPIReady() {
    ytPlayer = new YT.Player('media_area', {height: '%d', width: '%d', videoId: '%@',
        events: {'onReady': onPlayerReady}
    });
}

function onPlayerReady(e) {
    e.target.playVideo();
}
</script>
</html>

So, basically, the UIWebView shows the video thumbnail. Then I have to tap on it to begin playback. Autoplay is not working. Any idea what I might be doing wrong?

RajV
  • 6,860
  • 8
  • 44
  • 62

2 Answers2

14

Yes, you can make it autoplay by setting mediaPlaybackRequiresUserAction to NO. This is a project based on your HTML file, and it does autoplay after the app launch.

https://dl.dropbox.com/u/17350105/YoutubeAutoPlay.zip

Edit
As @RajV mentioned in the comment, you should use loadRequest: instead of loadHTMLString: for some reason I can't explain. Here is a working example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    [webView setMediaPlaybackRequiresUserAction:NO];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"YT_Player" ofType:@"html"] isDirectory:NO]]];
    [self.view addSubview:webView];
}

By the way, if you want to play youtube video inline, you can follow this answer I posted few days ago. (Oh, I just spent so many hours on Youtube iFrame API these days...)

Community
  • 1
  • 1
iForests
  • 6,757
  • 10
  • 42
  • 75
  • 2
    Thank you so much for posting fully working code. It appears that autoplay works if you load content using loadRequest. It doesn't work if you use loadHTMLString. Can you please verify this and modify your answer. I will then accept it. – RajV Mar 21 '13 at 23:03
  • It was working perfectly a few days ago, but all of a sudden it does not work today! Any idea why? – xcoder Dec 10 '13 at 19:32
  • the problem with this solution is that it reads off a static file with a hardcoded youtube video id.. – abbood Mar 20 '14 at 12:06
2

UIWebView does not respect autoplay because of Apple's concerns over mobile bandwidth use age (I.e a video begins playing when the page is loaded, and the user is on a cellular network, and it gobbles up their data). You'll either have to access the DOM (if possible on iOS, not sure) and trigger it manually or work around it.

SevenBits
  • 2,836
  • 1
  • 20
  • 33
  • 1
    You are right. Here is the reference from Google. https://developers.google.com/youtube/iframe_api_reference#Mobile_considerations – RajV Mar 12 '13 at 19:21