After playing a youtube-video in an UIWebView, the app restarts - ios

i'm using the following code, so load a Youtube video in my UIWebView (Outlet):
NSString *videoUrl = [self getVideoURL];
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 280\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"280\" height=\"156\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"280\" height=\"156\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[videoView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
all this works fine. but after watching the video and tapping on "done", my app starts new from beginning... but i don't understand why.
hope someone can give me a hint why this is happening?

ok I found my mistake:
the "bug" is at the youtube player. After pressing "done", it returns to my rootView Controller. In my case, this is the really first view of the app.
I solved this by setting my ModalView as my rootViewController after presenting it.

Related

How to open youtube in webview in ios7

I'm trying load youtube in UIWebview in iOS7 device using embedded string,its not loading.
But in simulator and below iOS7 devices are working good.
Please suggest me how to solve this.
I'm using like below,
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = yes, width = 320\"/></head><body style=\"background:#00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"180\"><param name=\"movie\" value=\"http://www.youtube.com/v/%#&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://www.youtube.com/v/%#&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"768\" height='%0.0f'></embed></object></div></body></html>", strurl, strurl,webview.frame.size.height];

How to get a video to play in UIWebView (video is in a ALAsset group)?

Okay maybe the headline is somehow disturbing, I hope to state this so you can understand my problem.
So I have a scrollview which has paging enabled. I want to build something like iOS Photo App.
I load the photos and videos which are on the device via ALAsset and store them in a group.
Now i want to present each object on a separate subview of the scrollview.
For the pictures it is working fine. But for videos I cant make it work.
Here is what i did and what did not work:
First:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString *embedHTML = [NSString stringWithFormat: #"<html><head><style></style></head><body>\
<video id='video_with_controls' width=\"%0.0f\" height=\"%0.0f\" controls>\
<source src='%#' type='video/MOV'/></video><ul>\
</body></html>", self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height, [assetRepresentation url]];
[webView loadHTMLString:embedHTML baseURL:nil];
then:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString* embedHTML = [NSString stringWithFormat:#"<video controls> <source src=\"%#\"> </video>", [assetRepresentation url];
[webView loadHTMLString:embedHTML baseURL:nil];
Also I tried to store it in NSData first and then load it from there, but that causes memory problems and wont work.
Any help is highly appreciated!
Edit: As I think UIWebView is the thing that matches the iOS App visually at most, I want to use this and not for example the MPMoviePlayer .. if I am wrong there I would be glad to know how / what else looks like the original App
I guess the video url is local thats why its not working in UiWebView
so the solution would be this:
in delegate method webviewdidstartload,set up MPmovieplayer controller to play inside a UiView and yes it will look like original app as you will be playing it in a view
something like this:
Add MPMoviePlayerController's view to a UIView
When you movie file is located locale you must set the baseURL to your MainBundle and put the full path in the "src"-tag.

How to change embed video size while playing in UIWebView?

I have a UIWebView with size 250x160, which should play video from youtube.com.
NSString *videoURL = #"http://www.youtube.com/v/Y4Y_a45Bv20?version=3&f=videos&app=youtube_gdata";
NSString *videoHTML = [NSString stringWithFormat:#"\
<html><body>\
<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"%f\" height=\"%f\">\
</embed>\
</body></html>", videoURL, self.web.frame.size.width-20, self.web.frame.size.height-20];
[self.web loadHTMLString: videoHTML baseURL: nil];
While playing video I want to resize the UIWebView. When I make it, for example 2 times bigger, embed video stays how it was 250x160. How can I resize it without restarting it?
I found anoyingly easy solution :
NSString *videoHTML = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"98%%\" src=\"http://www.youtube.com/embed/Y4Y_a45Bv20?showinfo=0\" frameborder=\"0\"></iframe>"];
The key is in width=\"100%%\" and height=\"98%%\". I didn't set height to 100%%, because while playing video, it's strangely increases and after 10-20 seconds you need to scroll uiwebview to find where video is gone.
And don't forget set UIWebView scalePageToFit = YES;
try affineTransform.
webview.transform = CGAffineTransformMakeScale(2, 2);
or
webView.scalesPageToFit=YES;
I found this approach and it worked for me:
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
Auto-resize iOS WebView Helper

How can I embed a Youtube video in a iOS6 app?

I have problems embedding Youtube videos in a iOS6 app.
The problem is that whenever I create a html element that uses the old / or new embed code, it opens Safari and shows the video
I use the Youtube API to get videos, and use PhoneGap
Update
The solution is to set OpenAllWhitelistURLsInWebView to YES in Phonegap settings
You can use UIWebview to load videos instead of Safari.
Here is a sample code
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://www.youtube.com/v/oHg5SJYRHA0";
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=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
[window addSubview:webView];
[webView release];
Not shure if you are talking a real APP (dont know anything about that)
but if its a WEBAPP (a normal webpage that tries to look as a real APP),
Then the only way i found to prevent youtube-videos from opening in Safari/Qt-player
is to embed it using "Fancybox" (maybe other boxes also).
that will keep it on the page in Youtube´s own player.
You can even have a original playlistplayer.
I stopped trying to embed youtube videos on iPhone-pages (to small)
I tried to include a link to a jSfiddle-example but this forum wont accept it
it forces me to write the whole example here...which i´m not going to do
because that is to much work for me right now.
(dont know how others are allowed to link to jSfiddle)
This forum is way to dictative to my taste.
Anyway you should be able to (incase of a webapp) find info at Fancybox´s website.

Embed Youtube videos :- with contains content from * , it is restricted from playback on certain site

How to embed a Youtube video that has copyrighted content in it.
For example when you try playing this video(http://www.youtube.com/embed/ADBKdSCbmiM) in a UIWebView it says
This Video Contains content from Vevo. It is restricted from playback on certain sites
How would I go about getting videos like this to play in an embedded player or a custom player that I would make using MPMoviePlayer or such. I know this is possible as following app does this. (http://itunes.apple.com/us/app/audioviz-view-your-songs-on/id547001249?mt=8)
Edit
Few videos play in browser but in iOS Simulator its showing
This video contains content from SME . It is restricted from playback on certain site
Thanks for all the help!
You can use player vars while initializing youtube sdk:
NSDictionary *playerVars = #{
#"origin" : #"http://www.youtube.com",
};
[self.playerView loadWithVideoId:#"KOvoD1upTxM" playerVars:playerVars];
Enjoy!
To play the youtube video using uiwebview:
First check if your youtube URL is played or not in browser. If the video doesn't play in the browser it won't play in the device either
You only check in the device; the video doesn't play in simulator
- (void) displayGoogleVideo
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://www.youtube.com/v/oHg5SJYRHA0"; // valid youtube url
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=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
[window addSubview:webView];
[webView release];
}
If you're embedding on a mobile app, you need to set your Referer in your request header for the video. Otherwise, Youtube automatically blacklists you if your Referer field is left blank.
I answered this in detail here. I was experiencing this myself and I fixed it in my case (iOS app using React Native). I can play Vevo, WMG, and all other restricted content now.

Resources