Play Youtube video in a UIWebView without full screen - ios

I would like to emebed a Youtube video in my app. But normal technique is, we embed a youtube video in a UIWebView and when user clicks, it automatically launches in a MPMoviePlayerController. But this launches in full screen. How to play this youtube video in a MPMoviePlayerController without going to full screen. I would like to display this in a half of the screen.

There are several ways to do that. In addition to setting:
videoView.allowsInlineMediaPlayback = YES;
The easiest and dirtiest way is to disable controls like this:
Method 1
Notice controls: 0
const player = new YT.Player("player", {
width: "100%",
height: "50%",
videoId: "[your video id]",
playerVars: {
controls: 0,
rel: 0,
modestbranding: 1,
html5: 1,
showinfo: 0,
},
});
Method 2
HTML Embedded iFrame code (notice the &controls=0 and/or &playsinline=1)
<iframe
id="ytplayer"
type="text/html"
width="100%"
src="http://www.youtube.com/[your_video_id]?autoplay=1&controls=0&playsinline=1&modestbranding=1&rel=0&showinfo=0&autohide=1&html5=1"
webkit-playsinline
frameborder="0"
></iframe>;
You only add &playsinline=1 (or playsinline:1 in the Javascript case next inside the playerVars)
In this case, user will still be able to go full screen, but the player should start normally in the borders of your view.
I hope this helps.

I'm using a UIView which is defined as a YTPlayerView to play the video.
I followed this tutorial: YouTube Tutorial to Embed Video
I created a dictionary. I then added it to the videoId definition (which video I'm playing).
From YouTube Tutorial:
Replace the loadWithVideoId: call with this code:
OBJECTIVE-C
NSDictionary *playerVars = #{
#"playsinline" : #YES,
};
[self.playerView loadWithVideoId:#"M7lc1UVf-VE" playerVars:playerVars];
Swift 2.1:
var playerVars = ["playsinline" : 1]
videoId.loadWithVideoId(["videoId"], playerVars: playerVars)
This will disable the full screen when trying to play the video, and it will also allow the user to go full screen if they want.
Hope that helps someone.

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

Wrap the video in html5 file and add webkit-playsinline in the attribute of <video> tag. Then set webView.allowsInlineMediaPlayback = YES; That works perfect for me.

There still doesn't seem to be a way to do this with MPMoviePlayerController, but the best workaround that uses a UIWebView is the YTPlayer open sourced by Youtube. You can get the source on Github, then you can just follow this tutorial.
I simply followed that tutorial and it works pretty well and is fairly customizable. Full screen plays in an AVFullScreenViewController which is a system standard, thus, it can be rotated for even better full screen viewing.

Related

Open iframe video in fullscreen programmatically

I open youtube video using iframe in webview. It shows default control buttons for video including fullscreen button. How can I call function of fullscreen button by myself? For example, I want go to fullscreen on landscape, but videos open in fullscreen only by tap on fullscreen button or while first loading. I need go to fullscreen while playing video.
Hope you are using YTPlayer for YouTube Videos.
If not you can find the player here - GithubLink
For YTPlayer, there's a parameter playsinline.
To always play fullscreen, you need to set the playsinline property to 0. Something like:
let playerVars = [
"playsinline" : 0
]
player.loadWithVideo(id:movieURL, playerVars:playerVars)
I have found function. There is webkitEnterFullScreen in webkit's video tag: https://developer.apple.com/documentation/webkitjs/htmlvideoelement
So all I need is to get video and execute function through javascript:
playerView.webView?.evaluateJavaScript("document.getElementById('player').contentDocument.getElementsByClassName('video-stream')[0].webkitEnterFullScreen()") { object, error in
print(error)
}

Disabling auto-play in full screen on iOS

The only problem i'm having is that i'm unable to disable playing videos on full screen according to apple documentation this is enabled by default and needs to be set as follows:
webView.configuration.allowsInlineMediaPlayback = true
Which is based on my understanding how it's supposed to be. However this doesn't work and even after configuration right after you press play on video it opens up in native full screen player. I'm using WKWebView.
Apple's Documentation for this
I faced with this problem using video tag in web view.
I have three videos in the page, and all videos successively played full screen.
I solved this problem by
1.Check Inline Playback
2.Add playsinline attribute to video tag
<video src="your_videosrc" autoplay playsinline></video>
Glad someones help :)
Playing a video inline requires two prerequisites:
Setting the configuration
Providing a correctly formatted link
1. Setting the configuration
The configuration of the WKWebView has needs to be set while initialising. Modifying allowsInlineMediaPlayback at a later point will not work:
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), configuration: configuration)
view.addSubview(webView)
2. Providing a correctly formatted link
In order for the web view to know that the video should start playing inline the appropriate URL parameter needs to be set.
For this you simply have to append ?playsinline=1.
Example:
webView.load(URLRequest(url: URL(string: "https://www.youtube.com/watch?v=OYbXaqQ3uuo?playsinline=1")!))
From Storyboad you can check/uncheck Inline playback to stop/allow full screen.
I'm having a similar issue, however it's a slight variation. We have a WkWebView that loads a mobile friendly web application. At various points there are links to videos which work great in that they do open the default iOS video player so the user can go full screen. That's all great. However we have a section where we have some podcasts. I'm trying to use the streaming provider's javascript player for these audio streams but when I press play the native iOS video player takes over. We don't want this because the provider's player has some features we need in it. I've tried adding the ?playsinline=1 but didn't have any affect on the playback. Is it possible to do this without affecting he video playback in the other areas of our app?

How to play Youtube Video from a browser app on non full screen in Objective C

I am working on Browser app, I want when user searches any video on youtube.com the video is playing on full screen but I want the video to be open on a fixed size small frame, Say Half of phone screen size.
I have Video url and video id. I have integrated YOUTube API but as soon as I hit play button on youtube player it plays on full screen.
Please find codebase of integration
self.playerView = [[YTPlayerView alloc]init];
self.playerView.frame = webView.frame;
[self.playerView loadWithVideoId:str];
[webView.superview addSubview:self.playerView];
Please find the screenshots
When youtube player is added and After tap on play button
Help will be much appreciated.
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
#Pyro
setAllowsInlineMediaPlayback works only for ipad, I have already enabled it. and it does now worked, It is not allowing to play the video.
Image after implementation

How to play YouTube video in vertical mode?

YouTube player update allow vertical video can display fullscreen in vertical mode. I was asked to fit YouTube video in a vertical view (not a full screen mode) in our iOS app.
We use YTPlayerView to playing YouTube video, but it doesn't provide any method to check if a video is vertical. I guess we can query such the info from YouTube web API, but I couldn't find any thing.
Has anyone tried to do the same thing like mine?
If it's not fullscreen mode then:
NSDictionary *playerVars = #{
#"playsinline" : #1,
};
[self.playerView loadWithVideoId:#"VIDEO_ID" playerVars:playerVars];
Just adjust the view's position etc, it will play just fine. Edit the html file they provide so the background matches your app's etc if you don't like the black.

iOS does not show/play video in UIWebView iframe

I use UIWebView with the:
var html:String = "<iframe src='http://46.101.234.111/trailers/suicide_squads_1' width='100%' height='200px' scrolling='no' frameborder='no'></iframe>"
cell.trailerView.loadHTMLString(html, baseURL: nil)
but it doesn't show/play the video on start. It just show play icon on white row and when I press on it - nothing happens.
iframe works, I checked it. What is the problem and how can I fix it?
Videos on the iPhone will not auto play from websites. Also make sure the video is in a format that can be played on an iPhone.

Resources