How to make YouTube video starts to play automatically inside UIWebView - iphone

I have video embedded in my application using this solution (html string in UIWebView):
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
Unfortunately client requires his own thumbnail. I would like to solve it by creating UIWebView once he taps on this thumbnail and play the video inside automatically.
How can I do it?

Try this:-
NSString *htmlString = [NSString stringWithFormat:#"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 50\"/></head>"
"<body style=\"background:#F00;margin-top:0px;margin-left:0px\">"
"<div><object width=\"50\" height=\"50\">"
"<param name=\"movie\" value=\"%#\"></param>"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"50\" height=\"50\"></embed>"
"</object></div></body></html>",[d objectForKey:#"hrefUrl"],[d objectForKey:#"hrefUrl"]];
[videoView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];

Following is the html string to play video automatically inside UIWebView...
NSString *embedHTML = [NSString stringWithFormat:#"<html><body><video controls=\"controls\" autoplay=\"autoplay\"><source src=\"%#\" type=\"video/mp4\"/></video></body></html>", url];
[_webView loadHTMLString:embedHTML baseURL:baseURL];
I have tried the following one, it is working fine and also starts to play automatically.
NSString *embedHTML = #"<html><body bgcolor="black"><embed id="yt" src="http://www.youtube.com/watch?v=9vyYHVB-QnY"type="application/x-shockwave-flash" width="320.00" height="370.00"></embed></body></html>";
[_webView loadHTMLString:embedHTML baseURL:baseURL];

Related

Iphone:Youtube video in UIWebview

I Want to show the the youtube video in an UIWebView and I am able to show the Video in an UIWebView with the help of following function to embed the YouTube URL.
- (void)embedYouTube:(NSString*)url
{
NSString *embedHTML = #"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width =\"%0.0f\"/></head><body style=\"background:transparent;margin-top:0px;margin-left:0px\"><div><object width=\"212\" height=\"172\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%0.0f\" height=\"%0.0f\"></embed></object></div></body></html>";
//NSString* html = [NSString stringWithFormat:embedHTML, url, self.videoView.frame.size.width, self.videoView.frame.size.height];
NSString *html=[NSString stringWithFormat:embedHTML,self.videoView.frame.size.width,url,url,self.videoView.frame.size.width,self.videoView.frame.size.height];
[self.videoView loadHTMLString:html baseURL:nil];
}
Here url is the youtube url,videoView is the webview.
Now this function works just good for the normal urls like http://www.youtube.com.... but for the mobile youtube urls like http://m.youtube.com.... ,it doesn't show the video, though the video can be played on the browser.
It is working if I replace the 'm' with 'www' from the url,but I dont think so it is good idea.

How to play embedded you tube video in iOS5

I am in trouble to sort the issue of playing the you tube video on my iphone 5...
I am using this code for this:
urls = [urls stringByAppendingString:#"&autoplay=1"];
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=\"212\" height=\"212\"><param name=\"movie\" value=\"%#\">",urls];
NSString *htm2=[NSString stringWithFormat:#"</param><param name=\"wmode\" value=\"transparent\"></param> <embed src=\"%#\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"212\"></embed> </object></div></body></html>",urls];
// NSString* embedHTML = #"<html><head> </head><body style=\"margin:0\"> <iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"%d\" height=\"%d\" src=\"%#\" frameborder=\"0\" allowFullScreen></iframe> </body></html>";
NSString* html = [NSString stringWithFormat:#"%#%#",htmlString,htm2];
NSLog(#"Html - %#",urls);
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if(version <= 5.0)
{
NSRange r=[urls rangeOfString:#"v="];
NSRange ran=NSMakeRange(r.location+2, [urls length]-r.location-2);
NSString *vid=[urls substringWithRange:ran];
html=[NSString stringWithFormat:#"<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=%#&fs=0\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>", vid];
}
//NSLog(html);
[videoView loadHTMLString:html baseURL:nil];
Can anybody please help to me to sort out with this issue?
Your help will be highly appreciated.
Have you using web view Delegate method for play video automatically ? if yes then remove code for playing video automatically. Check your HTML. Use balh instead of . One more thing please check video URL on browser confirm video exist or not j
follow the following links you will get this done
http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application

can I check youtube videos on simulator using this code?

I am using this code to see youtube videos
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// 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;
}
Now I am not able to see the relative video on the simulator..however white webview is coming there..
so is there anything like that that I cannot see youtube videos on simulator or there is any other problem in the code?
You can't see Youtube video in simulator. If white webview is showing, I think your code works.
Try installing on your iPhone/iPod touch.

Problem embedding an youtube video on iphone

I am trying to embed a video with the following code to avoid launching the youtube app and stay in my app:
-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[movieDescView addSubview:videoView];
}
[videoView loadHTMLString:embedHTML baseURL:nil];
}
The problem is when I clicked on the play icon, it opens the youtube website with my uiwebview asking to install some web app . I would like to prevent this web app pop up to show or start the video full screen. Actually, any solution to show a youtube video without exiting my app would do.
You can use this to load the movie (no needed to generate a html string):
movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];
#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *urlRequest = [request URL];
if ([[urlRequest absoluteString] isEqualToString:url]) {
return YES;
}
return NO;
}
The onely thing you have to looking for, is to use the "embed" link.
UIWebView *wbView = (UIWebView *)[self.view viewWithTag:1];
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,url, 64.0, 64.0];
[wbView loadHTMLString:html baseURL:nil];
"url" is your video url
You don't use embed if you are trying to play the video. Just use the normal video URL so it will open in the iPhone's YouTube application rather than the web. If it opens in the web it will ask to install it's web app.
You need to change your HTML to this:
Play Video
Or to launch it directly to the native app use the URL:
http://www.youtube.com/watch?v=j8Bc7eRTdWY
More Reference Information:
Apple Reference on URL Schemes
Apple Reference on Youtube Links
Tips:
stringUrl = [stringUrl stringByReplacingOccurrencesOfString:#"/watch?v=" withString:#"/embed/"];
where stringUrl is a video link of youtube ( ex. Youtube )
I spent quite a bit of time looking through libraries and old code snippets; this library worked for me:
https://github.com/larcus94/LBYouTubeView

How to embbed video on uiview or uiwebview

There are some apps in the app store that have a video stream without using mpvideo and they are embbeded on the uiview or uiwebview.
How is this implementation done? Any help is great thanks.
Probably with the standard HTML5 <video> tag. It allows you to play a video file that is local or remote. Or even a live stream. Check Apple's Safari Dev Center for all details.
You can try to load the HTML code into the webview itself with loadHTMLString:
The method is part of UIWebView and looks something like this:
[self.webview loadHTMLString:(NSString *)_some_string baseURL:nil]
You can store a basic html page with the embed code for the video in the _some_string variable and it will load up that page with the embed code.
NSString *html = #"\
<html>\
<head></head>\
<embed code for video here>\
</body></html>";
[self.webView loadHTMLString:html baseURL:nil];
This is a pretty complicated solution due to the way Apple has videos setup.
Create an HTML file with a basic HTML5 video player, with source="video/you/want". Be sure to add webkit-playsinline to the video.
<video width=“320” height=“240” webkit-playsinline><source src=“sample_iPod.m4v”/></video>
Add this player.html as a resource to your Xcode project.
Create a UIWebView, hook it up in the Storyboard editor, and then do the following in ViewDidLoad:
//set the webView delegate - hook up in Storyboard as well!
self.webView.delegate = self;
//initialize our HTML path - the file is player.html in our project
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#”player” ofType:#”html”];
//encode the path
NSString *escapedPath = [htmlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//webview needs NSURL, so let’s make one
NSURL *url = [NSURL URLWithString:escapedPath];
//load the request in the webview
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
Finally, the key step, be sure to flag the view to allow inline playback.
//flag the webview to play inline
self.webView.allowsInlineMediaPlayback = YES;
I wrote a post with more details here.
Can you try this one.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];
[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}