not embedding some youtube video in Xcode - objective-c

I embedded youtube video in my app, like it is show on many tutorials
NSString *htmlString = [NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed src=\"%#\"\
type=\"application/x-shockwave-flash\" width=\"180\" height=\"140\"></embed>\
</body></html>", videoLink];
[webView loadHTMLString:htmlString baseURL:nil];
Why I can play video like this www.youtube.com/watch?v=FMueitNN8HI
and not like this www.youtube.com/watch?v=jsFV1A8l5e4 ?
In the second case it shows a webview with button play crossed out like this
Where is the problem?
Thanks

This is because the owner of the account that uploaded the second video chose not to make the video mobile compatible. Here is a screenshot of your second video open on my computer with the browsers user agent set to iPhone.

Related

UIWebView breaking constraint when playing a video in fullscreen (Xcode 6)

I'm updating an iOS app for an YouTube channel. In the previous version (iOS 7 using Xcode 5), I used to embed youtube iFrame in a UIWebView and everything would work just fine.
However, in iOS 8 using Xcode 6, every time I play a Youtube video and it goes full screen, my UIWebView constraints break and UIWebView gets relocated in my Controller (usually goes 10 points up in the screen).
I've tried to change the constraints but it seems that doesn't matter what constraint I set up, it will eventually break when the player goes full screen.
Here's the html code used to embed YouTube iframe:
- (void)embedYouTube:(NSString *)urlString
{
NSString *embedHTML =[NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: blue;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"140\" width=\"325\" src=\"http://www.youtube.com/embed/%#\"></iframe>\
</body></html>",urlString];
[self.webView loadHTMLString:embedHTML baseURL:nil];
}
Since the log is huge, I've pasted it in pastebin: http://pastebin.com/z4zyq7Hs
Thank you :)
I had same problem. But, I do not experience relocation problem though (using Xcode 6.1), only bunch of constraint breaking logs.
What I tested that still cannot work:
- remove constraint and setTranslatesAutoresizingMaskIntoConstraints to YES for all the webview and its subview
- swap UIWebview with WKWebview, the newer and faster UIWebview
I also test playing Dailymotion video and it also has same problem, so I suspect this is more the problem on MediaPlayer views of UIWebview.
try like this
- (void)embedYouTube:(NSString*)url 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, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
I'm about to leave work so I'll have to type this answer quickly,
one way I have found of fixing this is refreshing the cache of the page being viewed, I don't have a solid fix for this but I came up with a temporary one last week, this can be done by creating a link to a css file in the header of your html
<link id="refreshCache" rel="stylesheet" type="text/css" href="refresh.css">
it's important that you also create a refresh.css file, you are however aloud to leave it blank,
now using javascript or jQuery if you already have it linked up, use the
.remove
http://api.jquery.com/remove/
on the id refreshCache, on page load, removing the link in the header to the empty file when the page loads...
this will refresh your browsers cache, hope this helps!

Youtube crash when press uiwebview

I have some problem with Youtube video in uiwebView. When I tried to play a video, the application is crashed. But no error description. This is only the case in iOS 7. In iOS 6 it works fine.
I create test app with this code its work too. But in my app is not playing.
Where there may be an error? anyone has ideas?
here is my webView:
NSString *embedHtml = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
body {background-color: transparent;}\
</style>\
</head>\
<body style=\"margin:0\">\
<iframe src=\"http://www.youtube.com/embed/%#\" width=\"%f\" height=\"%f\"></iframe>\
</body>\
</html>", #"l3Iwh5hqbyE", self.testView.bounds.size.width-3, self.testView.bounds.size.height];
[self.testView loadHTMLString:embedHtml baseURL:nil];

How to align UIWebView video (from online DB) to center : Xcode

I am displaying a video to my UIWebView from an online Database as:
//-------- Creating webview --------//
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(10, 145, 300,190)];
webview.scalesPageToFit = YES;
webview.backgroundColor = [UIColor redColor];
webview.opaque = NO;
[self.view addSubview:webview];
//-------- Loading Video --------//
NSURL *fileURL = [NSURL URLWithString:
[NSString stringWithFormat:#"http://mysite.org/myProject/sample.php?name=123746"]];
NSURL *nsurl=fileURL;
[webview loadRequest:nsrequest];
It is working great. But the video looks like below:
So, Is there is any way to align the video to center of the web view? Anyone please help. thanks.
3 Options.
If the video will always be that size, make the UIWebView just slightly bigger and center that.
Use [webView stringByEvaluatingJavaScriptFromString:#""] to pass in javascript to adjust the css to center it.
If its a webpage you control, add css to the HTML to center the video div in the window
as I was not able to find any good inbuilt solution using Xcode, I have solved the issue with the help of backend and HTML iframes. I've saved the exact url in a data base and using HTML <iframe>, I loaded the video in the webview with tag to the iframes.

Youtube in iOS5 - done button Tapped

In iOS5 i want to load Youtube movies, and i done it with:
- (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];
videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
}
calling by:
[self embedYouTube:#"http://www.youtube.com/v/PqtUSSdf8b4" frame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
And i have standard Youtube frame with "done" button. When i hit done button i'm redirect to my root VC. why? how can i get to my VC (the one when i'm calling it) ?
And more important - how to catch "done button" event ?
EDIT:
I have MainVC and DetailVC. MainVC calls DetailVC as a ModalViewController. From there I call this embedYouTube method. When i'm hitting done on this screen:
i'm returning to MainVC not DetailVC. Why? How to catch this event?
EDIT:
And iOS 6 displays only black background - nothing. Why?
The fullscreen video player will dismiss when you tap the done button, this is encapsulated into that full screen player which the system automatically presents as part of this youtube/flash safari embed trick. You shouldn't try to do anything when this is used.
About iOS 6 - iOS 6 no longer supports this way of embedding YouTube videos in your application (source)
As of iOS 6, embedded YouTube URLs in the form of
http://www.youtube.com/watch?v=oHg5SJYRHA0 will no longer work. These
URLs are for viewing the video on the YouTube site, not for embedding
in web pages. Instead, the format that should be used is described
here: https://developers.google.com/youtube/player_parameters.
The behavior of "DONE" button by default is that it dismisses all the modal view controllers, as you are presenting the DetailVC modally, so its also dismissed, you need to change the presenting method of DetailVC. Try simply by pushViewController.

How to play YouTube Movie on an iPhone Application when tapped on UITableViewCell?

I'm a newbie to iPhone development and just started on developing an application that contains a UITableView where each cell consisting of youtube video thumbnails in the form of webviews. For embedding YouTube Player on iPhone, I have used the follwing piece of code.
- (void)embedYouTube:(NSString*)url 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, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}
Now that I can see the thumbnail on the tableviewcell and once I tap on the thumbnail, YouTube player opens and play the movie.
My thumbnail occupies only a small portion of the cell and the rest of the area of the cell contains some text descriptions. My problem is that I must tap exactly on the thumbnail for the movie to play. If I tap somewhere else on the cell then it wouldn't play because my thumbnail doesn't extend all over the cell.
Isn't there a way to make the movie to play in didSelectRowAtIndexPath? I have seen some chaps suggest using Javascript but nobody seem to have an idea on the correct way of using it for this problem.
Highly appreciate it if anybody can help.
I suggest you to take a look at YouTube APIs and try to figure it out what is your real problem. but another good link is How To Play YouTube Videos Within an Application. Hope I'm helping you.
It's maybe not the best solution but It works:
//get your UIWebview in tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//...
videoView.mediaPlaybackRequiresUserAction = NO;
[videoView loadHTMLString:[self generateHtmlEmbedYouTube] baseURL:nil];
From the Documentation
mediaPlaybackRequiresUserAction:
A Boolean value that determines
whether HTML5 videos can play
automatically or require the user to
start playing them.
The onely problem with this solution is that you have to reload your UIWebView.