2

Possible Duplicate:
Embedding YouTube videos on iOS

I am trying to embed youtube videos on iOS, so that when a user clicks a button, youtube video is shown without leavign the app, and once the video is done, the user is returned to the app. I found a tutorial online showing how to do this, and i followed it. I create a YouTubeView:

#import "YouTubeView.h"


@implementation YouTubeView

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame {
    self = [super init];
    if (self) 
    {
        // Create webview with requested frame size
        self = [[UIWebView alloc] initWithFrame:frame];

        // HTML to embed YouTube video
        NSString *youTubeVideoHTML = @"<html><head>\
        <body style=\"margin:0\">\
        <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
        width=\"%0.0f\" height=\"%0.0f\"></embed>\
        </body></html>";

        // Populate HTML with the URL and requested frame size
        NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];

        // Load the html into the webview
        [self loadHTMLString:html baseURL:nil];
    }
    return self;  
}

#pragma mark -
#pragma mark Cleanup

- (void)dealloc 
{
    [super dealloc];
}

@end

and I am instantiating it like this: (case 2)

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    SongInfoTableVC *infoVC;
    YouTubeView *youTubeView;
    switch (buttonIndex) {
        case 0:
            NSLog(@"hello");
            [self setLyrics];
            break;
        case 1:
            infoVC = [[SongInfoTableVC alloc] initWithStyle:UITableViewStyleGrouped];
            [infoVC setBook:data];
            [infoVC setTitle:@"Song Info"];
            [[self navigationController] pushViewController:infoVC animated:YES];
            [infoVC release];
            break;
        case 2:
            youTubeView = [[YouTubeView alloc] 
                                        initWithStringAsURL:@"http://www.youtube.com/watch?v=xli2E2i8GZ4" 
                                        frame:CGRectMake(0, 0, 320, 480)];

            [[[self navigationController] view] addSubview:youTubeView];
            break;
        default:
            break;
    }
}

but this bring up a blank white screen. What am I doing wrong? Here's the screenshot: enter image description here

Thanks

Community
  • 1
  • 1
user635064
  • 6,219
  • 12
  • 54
  • 100

1 Answers1

4

Try testing on a device. I don't think this method is supported in the simulator.

See here: Embedding YouTube videos on

Community
  • 1
  • 1
edc1591
  • 10,146
  • 6
  • 40
  • 63
  • Thanks, I tried it out on the device and it works there! – user635064 May 08 '11 at 06:31
  • Hello i had same problem and now i succeed to play video in device but there is another problem that video is only played in ios version 5.1.1 , if i want to play video in below ios version 5.1.1 what should i do? – Birju Sep 18 '12 at 07:30