自动播放youtube视频:Objective-C

时间:2013-07-15 07:16:00

标签: ios objective-c youtube

我希望使用Objective-C从特定时间自动播放YouTube视频。 我使用了以下内容:

    - (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];

}

但视频没有自动播放。我哪里出错了? 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

我在自动播放YouTube视频方面遇到了类似问题 您可以在here

找到我的解决方案

别忘了将UIWebView mediaPlaybackRequiresUserAction属性设置为NO。

答案 1 :(得分:0)

可能你应该像这样使用它 -

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];

然而,它可能需要点击播放按钮(我不知道它是否自动播放)但仍然会将控件返回给应用程序。