Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Play Youtube Video on App

I am trying to explore what else can i do in iOS app development and it just now that i've tried to include a video on my app.

I have this code below that aims to play a youtube video when the view loads, but all i got is just a black webView.

NSString *videoURL = @"http://youtu.be/Wq_CtkKrt1o";

videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
videoView.backgroundColor = [UIColor clearColor];
videoView.opaque = NO;
videoView.delegate = self;
[self.view addSubview:videoView];


NSString *videoHTML = [NSString stringWithFormat:@"\
             <html>\
             <head>\
             <style type=\"text/css\">\
             iframe {position:absolute; top:50%%; margin-top:-130px;}\
             body {background-color:#000; margin:0;}\
             </style>\
             </head>\
             <body>\
             <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
             </body>\
             </html>", videoURL];

[videoView loadHTMLString:videoHTML baseURL:nil];
like image 270
Chano Tarongoy Avatar asked Mar 19 '13 03:03

Chano Tarongoy


People also ask

Can I play youtube video in AVPlayer Swift?

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. You can handle youtube videos in your own WebView. You can use IFrame Player API. Save this answer.

What is youtube iOS player Helper?

The youtube-ios-player-helper is an open source library that helps you embed a YouTube iframe player into an iOS application.

How do I link youtube to Swift?

Adding the code to play youtube videos in swiftYou will need a youtube video link to play in the player. After that, in the storyboard add a UIView that will be used to show the player, please check the below image. In addition, assign the WKYTPlayerView class to the UIView and create an outlet in your swift file.


2 Answers

you have to use embed link

use below code

NSString *videoURL = @"http://www.youtube.com/embed/Wq_CtkKrt1o";

instead of

NSString *videoURL = @"http://youtu.be/Wq_CtkKrt1o";

try this your problem will solve

like image 94
Pratik Avatar answered Jan 05 '23 02:01

Pratik


Can you try the code below it works fine for me

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </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, urlString, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
}
like image 43
Vinodh Avatar answered Jan 05 '23 01:01

Vinodh