Play Youtube video in a UIWebView without full screen
you set allowsinlinemediaplayback. but this feature on iPad. in iPhone not applicable. If you try play video with uiwebview on iPhone it will be played in full screen mode.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
How to embed YouTube video on iOS and play it directly on UIWebView without full screen?
If anyone is still facing this problem, below is by far the best solution I have seen. Works like a charm.
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10,300, 200)];
[self.webView setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.view addSubview:self.webView];
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>", 300, 200, @"JW5meKfy3fY"];
[self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
Source: https://code.google.com/p/gdata-issues/issues/detail?id=5204
Can I play a youtube video in a UIWebView inline (not fullscreen)?
Yes you can, you need to set property on UIWebView
webView.allowsInlineMediaPlayback=YES;
And you need to add &playsinline=1 to YouTube iframe embedding code.
<iframe webkit-playsinline width="200" height="200" src="https://www.youtube.com/embed/GOiIxqcbzyM?feature=player_detailpage&playsinline=1" frameborder="0"></iframe>
Tested on iPhone 4S running iOS 6.1.2 works like a charm.
How to play Youtube Video from a browser app on non full screen in Objective C
Does you project have Controller named MainViewController
, that controller should have issue with YTPlayerView
, please check for this view in your view controller.
in most of the cases it will not defined in the class you have directly use in the code
Searching for the issue in your code i found a property of UIWebview which will help you in playing video inline setAllowsInlineMediaPlayback
With help of this you can directly play the youtube video in the webview,
Here is code sample which you can use to play youtube video
NSString *strVideoId = @"M7lc1UVf-VE";
NSString *videoURL = [NSString stringWithFormat:@"https://www.youtube.com/embed/%@?feature=player_detailpage&playsinline=1",strVideoId];
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\"></iframe>\
</body></html>",videoURL];
[self.mainwebview setAllowsInlineMediaPlayback:true];
[self.view addSubview:self.mainwebview];
[self.mainwebview loadHTMLString:embedHTML baseURL:nil];
In this code you can pass any video id which you were passing in the loadWithVideoId
of YTPlayerView
To change the width/height you can make change in the width=\"100%%\" height=\"240px\"
portion of the iframe
It is working for me in the iPhone also which is looking like this:
iframe youtube play in iPhone
Another reference the setAllowsInlineMediaPlayback
working in iPhone: https://stackoverflow.com/a/15189889/4557505
The image which you shown in the screenshot looks like particular video issue
Note This code does not require YTPlayerView
so if this code works for you, you can remove the YTPlayerView
from your project if this is not used anywhere else
Update Some video not playing:
I think this issue has not nothing to do with the specific implementation as for me it's not working in browser(with embed Url which we are setting in iFrame) or YTPlayerview also
But till than i check and may be find some issue in implementation and as you have mentioned:
Yes all videos worked on YTPlayerView. These videos work on normal
full screen mode but not with embedded player
If you want to make implementation in the YTPlayerview you can check the following code:
NSString *strVideoId = @"3bMYgo_S0Kc";
self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height/2);
[self.playerView loadWithVideoId:strVideoId playerVars:@{@"playsinline":@1}];
[_mainwebview addSubview:self.playerView];
[self.mainwebview setAllowsInlineMediaPlayback:true];
[self.view addSubview:self.mainwebview];
In the loadWithVideoId
we can provide the inline parameter which can solve the problem of full screen and this may work for you on the iPhone also
Note this video is not working on YTPlayer or in browser(embedded url) for me but based on your comment you can try this
How to play a youtube video on iphone, not in fullscreen
Use this code for adding the you tube into your application.
For this you need to setup a webview in your view.
Then call the loadHTMLString:baseURL:
method on the UIWebView
instance with some carefully constructed HTML that contains the YouTube embedded player code snippet and some supporting HTML to make sure that the video thumbnail appears correctly. Set the base URL to the URL of your website (it doesn't do anything here -- ordinarily UIWebView
uses it to handle relative URL links correctly).
NSString *youTube = @"<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=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";
[webView loadHTMLString:youTube baseURL:[NSURL URLWithString:@"http://www.your-url.com"]];
For further reference : YouTube
Disable full screen youtube video display using UIWebView on iPhone
You can't, the YouTube video player on the iPhone will always go full screen.
If you can get the URL of H264 version on you tube you can use the Media Player framework for playback.
Embedded videos inside UIWebView dont fit the bounds of full screen, only on iOS10
if on iOS 10,use wkwebview,otherwise uiwebview,if else solve it
Related Topics
Uilocalnotification Is Deprecated in iOS 10
Add Entry to iOS .Plist File via Cordova Config.Xml
How to Prevent Duplicate Entry on Parse
How to Present iOS Uiactionsheet in Swift
Swift - Check If a Timestamp Is Yesterday, Today, Tomorrow, or X Days Ago
Passing Data to View Controllers That Are Embedded in Container Views
iOS Nsdate() Returns Incorrect Time
How to Implement Lazy Loading of Images in Table View Using Swift
Writing Handler for Uialertaction
Uicollectionview Cell Subviews Do Not Resize
Which Has Faster Performance Indexesofobjectspassingtest or Filteredarrayusingpredicate
Difference Between Dispatchqueue.Main.Async and Dispatchqueue.Main.Sync
How to Use Autolayout to Set Constraints on My Uiscrollview
How to Fetch All Contacts Record in iOS 9 Using Contacts Framework