iOS play any youtube video embed - ios

I want to add youtube player in iOS project. Sometimes some approaches works some not. Some approaches throw restrictions errors that this video will not play, go on youtube etc.Some approaches enters in fullscreen while entering play mode. I want youtube player in specific screen only.I have added this code and it fulfils my problem.
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='%d' height='%d' src='http://www.youtube.com/embed/%#?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
</body>\
</html>", width, height, #"VIDEO_ID"];
[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];

Related

Using a web view to play videos only from youtube

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>";
used this script to play videos and its working fine with iPhone but not with iPad. frame not get set.
NSString* html = [NSString stringWithFormat:embedHTML, str_url];
[web_View loadHTMLString:html baseURL:nil];
[web_View scalesPageToFit];
web_View.scrollView.bounces=NO;
Perhaps, there is need to reinvent the wheel now, you can use YTPlayer control for iOS. I am using it and it works fine. Even if you are stick with your solution it can help you on this issue.
Just one thing about YTPlayer, it does not support seek bar option.
Other ready-made options

_itemFailedToPlayToEnd when playing local mp4 using UIWebView

I am trying to play a local mp4 video using html5 video as follows.
<video id="media" controls="true" preload="auto" src="xxxx.mp4">
</video>
but its does not play. I get following error
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Any suggestion for playing video in UIWebView will be appreciated.
What you can do, so that you aren't hosting the video on your website is post the video to youtube, and embed it into your website so that way all you do is copy and paste the embedded code into your html...or you can do this:
- (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];
}
Try This, Should do the trick

Embed youtube video with subtitle into iphone app

I'm trying to embed YouTube video into iphone app and using UIWebView and loading embed code from youtube as html string. but i want to control the subtitle show/change or not.
this is my code
you2WebView = [[UIWebView alloc]initWithFrame:CGRectMake(10,70, 300, 300)];
you2WebView.allowsInlineMediaPlayback = YES;
you2WebView.mediaPlaybackRequiresUserAction = NO;
[self.view addSubview:you2WebView];
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='%d' height='%d' src='http://www.youtube.com/embed/%#?rel=1&playsinline=1' frameborder='0'>\
</body>\
</html>", 300, 200, #"MOqfyGNNKvo"];
[you2WebView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
and this is my button action code
-(void)clickedProgramBtn01{
[you2WebView stringByEvaluatingJavaScriptFromString:#"ytplayer.getOptions('cc')"];
NSLog(#"clicked!");
}
but it doesn't show the subtitle on video :'(
i want to show the subtitle on youtube video when i clicked ProgramBtn01
what should i do?

flv video URL streaming in iPhone Application

Thanks in Advance.
I want to play flv video in my Application,How can I do it?is there any code to play flv video from url in iphone Application?
I am using following code for this.
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, webViewObj.frame.size.width, webViewObj.frame.size.height];
[self.webViewObj loadHTMLString:html baseURL:nil];
Thanks
You can't play an flv video in your iphone. Apple doesnt support it. So if you want to play this videos, then convert it to another formats in the server side. That is the best solution for this problem.

iOS6 play videos with webView (works in iOS5)

I used to play videos inside my iOS app using uiwebviews with the following code:
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, urlToOpen, frame.size.width, frame.size.height];
self.videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.videoView setDelegate:self];
[self.view addSubview:videoView];
[videoView release];
With iOS6 it doesn't work.
Has anybody find any solution to play youtube videos compiling with iOS6?
Thanks
Ok, Here is the answer.
Use http://www.youtube.com/v/XXXXXXX
instead of http://www.youtube.com/watch?v=XXXXXXX.
find it here:
iOS - UIWebView not working due to parsing error
I just tested with iOS6 Simulator and iOS5 device, if you use the new embed code (iframe) then it works.
<iframe class="youtube-player" type="text/html" width="320" height="200" src="http://www.youtube.com/embed/VIDEOID" frameborder="0"></iframe>
Only downside is that the iframe contains a lot of dom elements that are downloaded.
Replace your code with the follow
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe title=\"YouTube Video\" class=\"youtube-player\" type=\"text/html\"\
width=\"%0.0f\" height=\"%0.0f\" src=\"%#\"\
frameborder=\"0\" allowFullScreen ></iframe>";
NSString *urlToOpen = #"http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=1";
NSString *html = [NSString stringWithFormat:embedHTML, urlToOpen, frame.size.width, frame.size.height];
self.videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.videoView setDelegate:self];
[self.view addSubview:videoView];
[videoView release];
As Youtube has discard the webview video and introduce iframe so You have to use this html for ios 6 and for ios 5 use the older one.
And remember that your url link for youtube video is should like
http://www.youtube.com/embed/video_Id?autoplay=1
where you can replace video_Id with your own video id.
If you use the embed in Phonegap you have set OpenAllWhitelistURLsInWebView to YES in Phonegap settings
Youtube with Webviews in iOS6 requires enabled cookies in the WebView. In order to do that, you need to add the following instruction in the AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
...
}
I tried this in simulator
[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.youtube.com/embed/b22cH2sfVcc"]]];
and it works
directly provide the embed src of youtube video to web view and it works.

Resources