0

I am using the below code to play youtube url:

NSString *youtubeUrl = [NSString stringWithFormat:@"http://www.youtube.com/v/%@",[photo videoUrl]];
[self embedYouTube:[youtubeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] frame:CGRectMake(10, 120, 300, 200)];

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame
{
    NSString* embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
        background-color: black;\
    color: black;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
    if(videoView == nil) {
        videoView = [[UIWebView alloc] initWithFrame:frame];
        videoView.layer.borderColor = [[UIColor blackColor] CGColor];
        [self.view addSubview:videoView];
        [videoView setHidden:NO];
    }
    [videoView loadHTMLString:html baseURL:nil];
}

User need's to tap on the webview to play this video. Can't it directly load the video without tapping on the play icon?

enter image description here

Dee
  • 1,887
  • 19
  • 47

4 Answers4

1

add a parameter autoplay=1 to the URL, like this:

NSString *youtubeUrl = [NSString stringWithFormat:@"http://www.youtube.com/v/%@?autoplay=1",[photo videoUrl]];
tkanzakic
  • 5,499
  • 16
  • 34
  • 41
  • Webview is unable to parse if I include '?' in the link so I have updated the link with slashes: http://www.youtube.com/v/%@/autoplay/1 ------ But it is not playing. how can I solve this issue? – Dee Jan 21 '13 at 09:16
  • can you `NSLog` the value of `youtubeUrl` after adding `?autoplay=1` an post the result? – tkanzakic Jan 21 '13 at 09:20
  • In browser it is playing properly, if I give this link http://www.youtube.com/v/_6wiHPxa-_U?autoplay=1 ----- When I test the app in iPhone it is not opening the video. it still requires tap on the play button. – Dee Jan 21 '13 at 09:44
  • Here is the html string: – Dee Jan 21 '13 at 09:45
  • 1
    are you using iOS6?, I read that Apple change the way the youtube's videos are handled and autoplay didn't work, try the other answer or check the answers in this [other question](http://stackoverflow.com/questions/12462052/youtube-video-playback-broken-on-ios6-works-fine-on-ios5) – tkanzakic Jan 21 '13 at 09:52
  • in that case you has to simulate the touch on the play button, you can try with the answer given by @TonyThomas or look for others options in the link I give you in my previous comment – tkanzakic Jan 21 '13 at 09:57
  • Yeah I am trying both the solutions. Thanx for you help. – Dee Jan 21 '13 at 10:01
0

Check this way to embed your play, which support Auto PLay

https://developers.google.com/youtube/player_parameters

Tony Thomas
  • 967
  • 10
  • 20
0

You should check LBYouTubePlayerController which uses the MoviePlayer component to load youtube videos and it's a lot easier to customize things, like autoplay feature.

estemendoza
  • 3,023
  • 5
  • 31
  • 51
0

I think the short answer is, if you're displaying videos with a webView autoPlay is no longer allowed per Apple guidelines. The docs specifically state:

Warning: To prevent unsolicited downloads over cellular networks at the user’s expense, embedded media cannot be played automatically in Safari on iOS—the user always initiates playback.

This same doc is referenced from Google Developer / YouTube API docs.

DenVog
  • 4,226
  • 3
  • 43
  • 72