user1452248
user1452248

Reputation: 767

Play Embeded YouTube video using UIWebview

Embeded Youtube video in UIWebview. How can i play this you tube video using UIWebview when UIButton is pressed.

- (void)viewDidLoad
 {
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

NSString *videoUrl = @"http://http://www.youtube.com/v/oHg5SJYRHA0";
NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%@\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%@\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl]    ;

[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];

//[window addSubview:webView];
[webView release]; 

[super viewDidLoad];
// Do any additional setup after loading the view.
} 

When user presses this UIButton it should start playing embedded youtube video

UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
 [videoButton addTarget:self action:@selector(VideoAction:) forControlEvents:UIControlEventTouchUpInside];
videoButton.frame = CGRectMake(0, 0, 40, 40);
UIImage *icn = [UIImage imageNamed:@"Video_icon.png"];
[videoButton setImage:icn forState:UIControlStateNormal];
UIBarButtonItem *video = [[UIBarButtonItem alloc] initWithCustomView:videoButton];

How can i play youtube video.

Thanks for help.

Upvotes: 0

Views: 3591

Answers (1)

Prajwal Runiyar
Prajwal Runiyar

Reputation: 51

Why dont you try this :

            [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=oHg5SJYRHA0"]]]
            [self.view addSubview:webView];

Upvotes: 1

Related Questions