3

I wish to autoplay a youtube video from a specific time using Objective-C. I used the following:

    - (void)viewWillAppear:(BOOL)animated
{
     [super viewWillAppear:animated];

     NSLog(@"%s",__FUNCTION__);

    appDelegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
    float width = 309.0f;
    float height = 196.0f;

    NSString *youTubeURL = @"http://www.youtube.com/embed/Gyf1kjaUZCo?autoplay=1";

    UIWebView *wv = [[UIWebView alloc] init];
    wv.frame = CGRectMake(0, 0, width, height);

    NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1];
    [html appendString:@"<html><head>"];
    [html appendString:@"<style type=\"text/css\">"];
    [html appendString:@"body {"];
    [html appendString:@"background-color: transparent;"];
    [html appendString:@"color: white;"];
    [html appendString:@"}"];
    [html appendString:@"</style>"];
    [html appendString:@"</head><body style=\"margin:0\">"];
    [html appendFormat:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/Gyf1kjaUZCo?autoplay=1\" allowfullscreen frameborder=\"0\"></iframe>", width, height];
    [html appendString:@"</body></html>"];

    [wv loadHTMLString:html baseURL:nil];

    [self.videoView addSubview:wv];

}

But the video doesn't autoplay. Where am I getting wrong? How do I solve this?

z22
  • 10,013
  • 17
  • 70
  • 126

2 Answers2

4

I had a similar problem with auto playing a youtube video,
You can find my solution here here

Dont forget to set the UIWebView mediaPlaybackRequiresUserAction property to NO.

Community
  • 1
  • 1
Eyal
  • 10,777
  • 18
  • 78
  • 130
0

May be you should use it like this-

NSString *htmlString =@"<html><head>"
            "<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>"
            "<body style=\"background:#FFFFF;margin-left:0px\">"
            "<div><object width=\"309\" height=\"196\">"
            "<param name=\"wmode\" value=\"transparent\"></param>"
            "<embed src=\"http://www.youtube.com/embed/Gyf1kjaUZCo?autoplay=1?f=user_favorites&app=youtube_gdata\""
            "type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"309\" height=\"196\"></embed>"
            "</object></div></body></html>";

        [webView loadHTMLString:htmlString baseURL:nil];

However it may need to tap the play button (I don't exactly know if it autoplays) but still it will return the control to the application.