Play youtube videos-ipad - ipad

In my application i want to play the Youtube URL(ipad).I'm using the below code.It simply show me a White screen
Youtube url is:http://www.youtube.com/watch?v=x61GMbKwMkE
This is my code:
NSString *embed = #"\
<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=320 height=460></embed>\
</body></html>";
NSString *embedURL=[NSString stringWithFormat:embed,appDelegate.videoURL];
NSLog(#"\n\n\n\n Myurl==%# %#\n\n\n",embedURL,appDelegate.videoURL);
[[WebView.subviews objectAtIndex:0] setScrollEnabled:NO];
[WebView loadHTMLString:embedURL baseURL:nil];
I'm creating and connecting UIWebView in XIB.
Anyone Guide me to do this
Thanks For your consideration and effort

Flash is not supported in iOS. Check out the new embed code for YouTube videos that will automatically choose the correct player for each environment.

Related

UIWebView embedded video fullscreen issue

I'm using ifame api to embed youtube video into UIWebView. I have a problem when closing the fullscreen mode - the player is translated to wrong position. Seems like the hide animation has some bug. The only solution i found is to place the UIWebView in top-left corner. I created a github repo with a simple project to demonstrate this issue.
https://github.com/demonoid67/youtube-video-sample . Can someone help me? Thanks in advangtage!
NSString* embedHTML = [NSString stringWithFormat:#"\
<html>\
<body style='margin:0px;padding:0px;'>\
<script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
<script type='text/javascript'>\
function onYouTubeIframeAPIReady()\
{\
ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
}\
function onPlayerReady(a)\
{ \
a.target.playVideo(); \
}\
</script>\
<iframe id='playerId' type='text/html' width='%f' height='%f' src='http://www.youtube.com/embed/%#?enablejsapi=1&rel=0&playsinline=1&autoplay=1&controls=1' frameborder='0'>\
</body>\
</html>", youtubeWebView.frame.size.width, youtubeWebView.frame.size.height, selectedVideoModel.youtubeId];
[youtubeWebView setAllowsInlineMediaPlayback:YES];
[youtubeWebView setMediaPlaybackRequiresUserAction:NO];
[youtubeWebView loadHTMLString:embedHTML baseURL: nil];
Maybe this will help you.

Embed youtube video in IOS8 does not fill up Webview width 100%

I have used following code for showing youtube video in iOS.
//Html string to display embed youtube video in native webview
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent; background:none;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed src=\"%#\"\
type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, http,viewRect.size.width, (viewRect.size.height*-1)];
videoView = [[UIWebView alloc] initWithFrame:CGRectMake(viewRect.origin.x , (viewRect.origin.y + viewRect.size.height), viewRect.size.width, (viewRect.size.height*-1))];
videoView.delegate = self;
//Stop Image Move
[videoView.scrollView setScrollEnabled:NO];
[self addSubview:videoView];
[videoView loadHTMLString:html baseURL:nil];
videoView.opaque = NO;
[self bringSubviewToFront:videoView];
I have assigned same width and height to both web view and the embed tag.Previously in iOS7, this code works well without any problem. But in iOS8 the youtube video width doesn't fills up the web view.Please I will really be a help, if someone advice on this issue.
Thanks in advance.

YouTube Embed player in Iframe doesn't work in iOS6

I really need your help. Working on an iOS app. I want to play youtube. I understood from reading many many blogs and posts, that we need to use an iframe in order to play a youtube video.
However, on some videos I get: "This video contains content from XYZ. It is restricted from playback on certain sites. Watch on YouTube"
I read this question: Youtube in iOS5 - done button Tapped which gives the link to youtube api: https://developers.google.com/youtube/player_parameters
They recommend to use the iframe. the example for youtube site is:
<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com"
frameborder="0"/>
The code I used:
<!DOCTYPE html>
<html>
<head>
<style>
* {
border:0;
margin:0;
}
</style>
</head>
<body>
<iframe webkit-playsinline id="player" type="text/html" width="320" height="180" src="http://www.youtube.com/embed/rEevIL1Wpcg?enablejsapi=1&playsinline=1&autoplay=1" frameborder="0">
</iframe>
</body>
</html>
Can someone help me understand it? I check the embedded flag to be true, they are all clips that are allowed to play on mobile devices.
Example for videos that works on the device:
http://www.youtube.com/watch?v=rEevIL1Wpcg&feature=youtube_gdata
http://www.youtube.com/watch?v=KzGe7pbGUiM
Example for videos that don't work on the device and bring up the error msg:
http://www.youtube.com/watch?v=1vhFnTjia_I&feature=youtube_gdata
http://www.youtube.com/watch?v=sxkiA0IjBZ0&feature=youtube_gdata
you can use webview as youtube player
Try Below Code it is working for me
in .h file
#property (strong, nonatomic) UIWebView *webView;
and in your .m file
NSString *videoURL = #"http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com";
// if your url is not in embed format or it is dynamic then you have to convert it in embed format.
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"watch?v=" withString:#"embed/"];
NSRange range = [videoURLString rangeOfString:#"&"];
#try {
videoURLString = [videoURLString substringToIndex:range.location];
}
#catch (NSException *exception) {
}
// here your link is converted in embed format.
NSString* embedHTML = [NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>\
</body></html>",videoURL];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:self.webView];
[self.webView loadHTMLString:embedHTML baseURL:nil];
Here you can change webview frame as you want and also can change videoUrl.
There are two concepts, embeddable and syndicated.
iOS devices use iframe so they basically embed.
Android devices that use player API can check syndicated.
When you do a search->list, you can set videoEmbeddable and videoSyndicated to true.
Or if you are iterating through videos, for each video, you can do a video->list call with video id and check status.embeddable in the response.
Here is a blog post about this topic, even though examples are in v2, information is still relevant.

How to access ios media player instance when youtube video is playing

I used below function for play youtube videos in iPhone using UIWebView
- (void)embedYouTube {
videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/xxxxxxxxxxxxx\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>"];
[videoView loadHTMLString:videoHTML baseURL:nil];
}
By this method video will play in the iOS media player. I wanted to hide controllers automatically, when the video is playing. Problem is how to access the media player instance.
I think you can try Subview concept, to access MoviePlayercontroller. Please refer this article here.

Removing scrubber and timestamp from YouTube video embedded in UIWebView

I have a YouTubeView in an app I am making and I am trying to disable some of the video controls that are appearing, I have seen another App do it where it only displays the Play/Pause and Fullscreen icons. Some of the videos I am rendering are quite small (200x150) so the scrubber is useless and the time is actually overlapping itself.
The function I am using is:
- (YouTubeView *)initWithStringAsYouTubeId:(NSString *)idString frame:(CGRect)frame;
{
if (self = [super init])
{
self.frame = frame;
self.delegate = self;
// HTML to embed YouTube video
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: transparent;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.youtube.com/v/%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSLog(#"%f", frame.size.height);
NSString *html = [NSString stringWithFormat:embedHTML, idString, frame.size.width, (frame.size.height*-1.0f)];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
It loads and plays fine but I just want to disable the scrubber and times, I have tried ?controls=0 with no luck.
Cheers,
Dean
To be able to hide the scrubber and other controls, remove your <embed> tag and replace it with its <iframe> equivalent, like this (I've set it up using the parameters/attributes from your example code):
<iframe id=\"yt\" type=\"text/html\" src=\"http://www.youtube.com/embed/%#?controls=0\" width=\"%0.0f\" height=\"%0.0f\" frameborder=\"0\"></iframe>
'controls=0' used with <iframe> will hide the controls for you.
In my brief experience with iOS embedded video, it seems like a solution to one problem often causes grief with something else that was previously working fine. Case in point: the above example hides the controls, but will overlay the video's title at the top-left of the UIWebView. The solution is to use this for your video's parameter list: '?controls=0&showinfo=0'
To see the scoop on the 'showinfo' parameter and others, check out the docs at: https://developers.google.com/youtube/player_parameters#Parameters Not everything in the parameter docs works in the iOS <iframe>, but play around with some of them and you might find something useful.
For what it's worth (and this might be obvious to expert web coders, which I am not), you don't have to assign the 'id' attribute to be 'yt', but most every example I've seen does this; 'yt' is no magic value, you can set the id to be whatever makes sense to you.
Is there a reason you don't use MPMoviePlayerController?
Using MPMoviePlayerController, you would simply create a UIView in your nib that is the size you want to player to be. Then alloc/init the player and set its frame to be the frame of the view. Using MPMoviePlayerController gives you access to a property to show/hide the controls. Additionally, you get all the great delegate methods telling you when video stops, if the users goes into or out of full screen, etc.

Resources