I want to play YouTube videos from my iPhone app. I have to tried play YouTube videos in my iPhone app with the following codes,
[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)];
- (void)playVideo:(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];
NSLog(@"%@",html);
}
But, this code is not working for me. And also i have tried with MPMoviePlayerController
the code is,
NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
This is also not working for me. Can anyone please tell me where i did wrong and suggest me any ideas? I want to play the YouTube videos in MPMoviewPlayerController
rather than using UIWebView
. Thanks in advance.
Just use XCDYouTubeVideoPlayerViewController. It seamlessly adds youtube video to your app without the need of
UIWebView
.It uses progressive download +
MoviePlayer
andAVFoundation
frameworks. Just supply it with a youtube video ID and you are good to go.It supports both full-screen & non full-screen and WORKS ON iOS SIMULATOR!!!
Example:
There is now a
YTPlayerView
class available with the YouTube Data API (v3).Have a look at Using the YouTube Helper Library to embed YouTube videos in your iOS application
It gives comprehensive instructions on how to install the helper library, and incorporate YTPlayerView into your storyboard.
Instead of the HTML embed code you are using, go to the YouTube website, find a video, use the share button and then the Embed button to get some example Embed HTML. Use that pattern for embedding the video rather than the Flash version you are currently using.
Play youtube video in swift 4.1
Add Pod pod 'XCDYouTubeKit'
import XCDYouTubeKit in ViewController
Mr.Yuvaraj.M your above code is correct. Please run the project in your iPhone/iPod touch device. The youtube videos may not support/not work in Simulator. Please check on this. Thanks.