I wrote the code as follows to play the videos in youtube embedded player .
It opens the player window and shows the vidoe , but does not play .
if(self.isYouTube)
{
NSString *embedHTML;
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: #"4.3" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
embedHTML = #"<html><head><style type=\"text/css\">body {background-color:black; color:black; margin-right:auto; margin-left:auto;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%#\" airplay=\"allow\" type=\"application/x-shockwave-flash\" position=\"fixed\" allowfullscreen=\"true\" autoplay=\"1\" width=\"320.0\" height=\"460.0\"></embed></body></html>";
} else {
embedHTML = #"<html><head><style type=\"text/css\">body {background-color:black; color:black; margin-right:auto; margin-left:auto;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" position=\"fixed\" allowfullscreen=\"true\" autoplay=\"1\" width=\"320.0\" height=\"460.0\"></embed></body></html>";
}
NSString *contentHtml = [NSString stringWithFormat:embedHTML,self.url];
[self.webView setBackgroundColor:[UIColor blackColor]];
[self.webView loadHTMLString:contentHtml baseURL:nil];
}
else
{
NSURL *urlForOpenFile=[NSURL URLWithString:self.url];
NSURLRequest *request=[NSURLRequest requestWithURL:urlForOpenFile];
[self.webView loadRequest:request];
}
can u tell me , what's the problem in the code.
Thanks.
iOS doesn't support flash, you should use the HTML 5's new tag video to wrap the video url like this:
<video src="point/to/mov"></video>
Related
I am using the below code to play youtube url:
NSString *youtubeUrl = [NSString stringWithFormat:#"http://www.youtube.com/v/%#",[photo videoUrl]];
[self embedYouTube:[youtubeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] frame:CGRectMake(10, 120, 300, 200)];
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame
{
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: black;\
color: black;\
}\
</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, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.layer.borderColor = [[UIColor blackColor] CGColor];
[self.view addSubview:videoView];
[videoView setHidden:NO];
}
[videoView loadHTMLString:html baseURL:nil];
}
User need's to tap on the webview to play this video. Can't it directly load the video without tapping on the play icon?
add a parameter autoplay=1 to the URL, like this:
NSString *youtubeUrl = [NSString stringWithFormat:#"http://www.youtube.com/v/%#?autoplay=1",[photo videoUrl]];
Check this way to embed your play, which support Auto PLay
https://developers.google.com/youtube/player_parameters
You should check LBYouTubePlayerController which uses the MoviePlayer component to load youtube videos and it's a lot easier to customize things, like autoplay feature.
I think the short answer is, if you're displaying videos with a webView autoPlay is no longer allowed per Apple guidelines. The docs specifically state:
Warning: To prevent unsolicited downloads over cellular networks at
the user’s expense, embedded media cannot be played automatically in
Safari on iOS—the user always initiates playback.
This same doc is referenced from Google Developer / YouTube API docs.
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
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];
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.
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