iOS - Force full screen video - iphone

I'm using a MPMoviePlayerViewController. When the video loads and starts playing it doesn't fill up the whole screen. I have to press the full screen button at the top right to make that happen. If I use MPMoviePlayerController I can't get it to fill the screen at all.
Any way via code that I can get my video full screen using the MPMoviePlayerViewController without having to press the fullscreen button?
I know i'm using a private API here, but thats ok, it is for a demo.
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] setOrientation:UIDeviceOrientationPortrait];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"phatpad" ofType:#"mov"];
if (moviePath)
{
NSURL *url = [NSURL fileURLWithPath:moviePath];
player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
}
player.view.frame = self.view.frame;
[self.view addSubview: player.view];
}
Here is a screen. The first is without pressing the full screen button, the second is after pressing it.

You have to set the fullscreen attribute to true and the scallingMode to the aspect fill like this:
if (moviePath)
{
NSURL *url = [NSURL fileURLWithPath:moviePath];
player = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
player.moviePlayer.fullscreen = YES;
player.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
}
I have tested at home and it works, so I hope to you too.

On iPhones and iPod Touches, MPMoviePlayerController plays in full screen no matter what, so I'm assuming you're talking about an iPad? Can we please see some code samples? I can make mine full screen simply by resizing the UIView in my XIB file. How are you trying to do it?

Related

How to play video from URL in Ipad?

In my application i get the links of video from server in UITableview .all these link are store on Sever in Textfile,i get all these link from sever one by one and assign each to cell in UITableview.all these i done succefully but i want when i click on any cell in UITableview its play the video in next view.here is my code.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
nextview *dvController = [[nextview alloc] initWithNibName:#"nextview" bundle:nil];
[self presentModalViewController:dvController animated:YES];
strFile = [Listdata objectAtIndex:indexPath.row];
NSLog(#"test=%#",strFile);
[dvController release];
}
and in next view i assign the link which i store in "strFile" to MPMoviePlayerController here is my code.
-(void)viewDidAppear:(BOOL)animated
{
NSLog(#"mytest=%#",strFile);
NSURL *url = [NSURL URLWithString:strFile];
NSLog(#"myurl=%#",url);
myplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
myplayer.view.frame = CGRectMake(0, 0,900, 700);
[self.view addSubview:myplayer.view];
[myplayer play];
}
In NSlog i see the link but its not play in MPMoviePlayerController.Any one can guide me that what mistake i make.thanx in advance.
#prince : can u tell me what url you are getting in NSLog?? i think u're mistaken in getting the url.
we can get url either by
url = [NSURL fileURLWithPath:strUrl];
else
url = [NSURL URLWithString:strUrl];
try it out once.
You probably shouldn't be setting the frame and adding to the subview, You may be confused with MPMoviePlayerViewController. Try this...
-(void)viewDidAppear:(BOOL)animated {
NSLog(#"mytest=%#",strFile);
NSURL *url = [NSURL URLWithString:strFile];
NSLog(#"myurl=%#",url);
myplayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
myplayer.scalingMode = MPMovieScalingModeAspectFill;
[myplayer play];
}
EDIT disregard my code, you can't directly play a YouTube video in MPMoviePlayerController. Check this Post instead Play YouTube videos with MPMoviePlayerController instead of UIWebView
to youtube video play inside app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. try this link:
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

How to add videoPlayer as an subview?

Hello I am facing an problem related to adding subview;
I am following the Code:
NSString *urlStr = [NSString stringWithFormat:#"http:x/iphone0.m3u8"];
NSURL *videoURL = [NSURL URLWithString:urlStr];
MPMoviePlayerController *iVideoPlayer = [[MPMoviePlayerController alloc]initWithContentURL:videoURL];
[self.view addSubview:iVideoPlayer.view ];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
iVideoPlayer.view.frame = CGRectMake(353,258,320,240);
}
else
{
iVideoPlayer.view.frame = CGRectMake(156,96,168,148);
}
[iVideoPlayer play];
In this code I like to add an video player as an subview . I have successfully add the videoPlayer.but problem is that during the video playing ,if i click on the specified area (CGRectMake(353,258,320,240);the video stop. I like apply the videoPlayer Function (next,Pause,Volume up/Down)which are not done .
How to resolve this .?
You have some options for the control style of your MPMoviePlayerController instance. By setting the controlStyle property to one of the following options, you can enable certain player controls (like pause, play, etc).
MPMovieControlStyleNone,
MPMovieControlStyleEmbedded,
MPMovieControlStyleFullscreen,
MPMovieControlStyleDefault
The descriptions for the above styles can be found here: MPMoviePlayerController Class Reference
Here is an example of how to set this property.
[iVideoPlayer setControlStyle:MPMovieControlStyleEmbedded]

Emulating splash video in iOS application

OK, there are very few options to emulate the splash video in iOS. All we can do is wait till application is fully loaded and then create Media Player and load video in it.
I implemented it with following code:
-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
NSLog(#"Intro video stopped");
[mMoviePlayer release];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURL* mMovieURL;
NSBundle *bundle = [NSBundle mainBundle];
if(bundle != nil)
{
NSString *moviePath = [bundle pathForResource:#"intro" ofType:#"mp4"];
if (moviePath)
{
mMovieURL = [NSURL fileURLWithPath:moviePath];
[mMovieURL retain];
}
}
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mMovieURL];
[mMovieURL release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer];
mMoviePlayer.controlStyle = MPMovieControlStyleNone;
[mMoviePlayer.backgroundView addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Splash/background.png"]] autorelease]];
mMoviePlayer.scalingMode = MPMovieScalingModeFill;
[window addSubview:mMoviePlayer.view];
[mMoviePlayer setFullscreen:YES animated:NO];
[window makeKeyAndVisible];
[mMoviePlayer play];
<... other stuff ...>
}
My video is only 1 MB. But this code do something different then I'd like to see:
First of all user can see a static splash screen for a few seconds;
Then a black screen appears for 1 or 2 seconds. I think this is happening because the media player is loaded.
Video start playing.
Main interface loads.
As you understand I don't like the pause with black screen - it looks ugly.
As far as I can see in my Console log the problem is that mediaplayer is waiting till the main view controller is fully loaded.
Few words about main view: i'm writing an application for iPad and the main view consists of several subviews with multiple images. Every image and every subview in main view loads some data from Internet Web service via ASIHTTPRequest lib.
I think that Media Player is waiting for all initial connections to finish and only then it's starting the video...
How can I force the video to play before main view is loaded? Or maybe I can delay the loading of main XIB?
You cannot get rid of the static splash image. While it is shown, the OS is loading the application and instantiating stuff until it is ready to call your UIApplicationDelegate. So all you can do is either use no splash (black screen for a few seconds) or make your movie start exactly with the shown splash screen so it looks like the static image would suddenly animate.
To get rid of the black screen while the movie loads, you can try to make the player transparent and have an UIImageView behind the player that shows the splash image. The behavior would be this:
Splash screen is shown (static image).
Application is loaded. You see the UIImageView, also showing the splash screen. On top of it is the transparent movie player.
Movie player finally has loaded the move and starts playing it.
At least in theory, this should cause the effect that the static image suddenly starts animating.
But if you don't use a splash screen at all (a lot of games do that), then it doesn't matter that the movie player is showing a black screen at first, you wouldn't notice.
Regarding showing the splash screen in an UIImageView: unfortunately, you have to test the interface rotation and load the image manually, there's no way to query which splash screen was shown. If you only support one interface orientation (again, a lot of games do this) you don't have this problem, of course.
There is a better solution now, assuming you are using UIViewControllers.
Instead of using MPMoviePlayerController, use MPMoviePlayerViewController. Here is some sample code, adapted from the question:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURL* mMovieURL;
NSBundle *bundle = [NSBundle mainBundle];
if(bundle != nil)
{
NSString *moviePath = [bundle pathForResource:#"intro" ofType:#"mp4"];
if (moviePath)
{
mMovieURL = [NSURL fileURLWithPath:moviePath];
[mMovieURL retain];
}
}
mMoviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:mMovieURL];
[mMovieURL release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer.moviePlayer];
mMoviePlayer.moviePlayer.controlStyle = MPMovieControlStyleNone;
[mMoviePlayer.moviePlayer.backgroundView addSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SplashCopy.png"]] autorelease]];
mMoviePlayer.moviePlayer.scalingMode = MPMovieScalingModeFill;
[window.rootViewController.view addSubview:mMoviePlayer.moviePlayer.view];
[mMoviePlayer.moviePlayer setFullscreen:YES animated:NO];
[mMoviePlayer.moviePlayer play];
}
Emulating splash video in iOS application
this code for swift4.0
var mMovieURL: URL?
let bundle = Bundle.main
if bundle != nil {
let moviePath: String? = bundle.path(forResource: "intro", ofType: "mp4")
if moviePath != nil {
mMovieURL = URL(fileURLWithPath: moviePath ?? "")
}
}
mMoviePlayer = MPMoviePlayerController(contentURL: mMovieURL!)
NotificationCenter.default.addObserver(self, selector: #selector(self.moviePlayBackDidFinish), name: .MPMoviePlayerPlaybackDidFinish, object: mMoviePlayer)
mMoviePlayer.controlStyle = .none
mMoviePlayer.backgroundView.addSubview(UIImageView(image: UIImage(named: "Splash.png")))
mMoviePlayer.scalingMode = .fill
window?.addSubview(mMoviePlayer.view)
// window?.rootViewController?.view.addSubview(mMoviePlayer.view)
mMoviePlayer.setFullscreen(true, animated: false)
window?.makeKeyAndVisible()
mMoviePlayer.play()
return true
}
#objc func moviePlayBackDidFinish(_ notification: Notification) {
mMoviePlayer.view.removeFromSuperview()
}

i cannot see movie playing in iphone simulator

i have created a application that plays movie on button click..my application is running without any warning or error. But i am not able to see the movie don't know why?
I have added the Media player framework and also imported the #import in viewController.h
my button action code is as follow...
-(IBAction)playMyMovie:(id)sender{
NSBundle *bundle =[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"jumps" ofType:#"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie =[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}
Please tell me what i am missing..
in iPhone simulator you could not play movie properly.
test the applcation on iPhone (Real instrument).
It will work fine.
You are creating a MPMoviePlayerController. It has a view property - that is its view. It is your job to put that view into your interface. If you don't, you won't see anything.
try add this line before the [theMovie play]
[self.view addSubview:theMovie.view];
and also remember to setFullScreen:
[self.theMovie setFullscreen:YES];
if you dont want fullscreen but want to embed into the view, you can ignore the "setFullscreen", but set the frame for the MPMoviePlayerController view:
[self.theMovie.view setFrame:CGRectMake("add in the size you want here")];

Embedded Video in a UIView with iPhone

I would like to write an app that downloads (or streams) a video (encoded as required) in a view. I dont want to use the MPVideoPlayer from the SDK as it opens the video in full screen. I would like to place another UIView (transparent) over the video so that my users can annotate over the video.
Anyone have any idea or can point me to some code that will play video in a UIView?
If you want to do this you will need to include your own (software) video decoder, which will not have access to the hardware acceleration on the system. Even if you can get it working with acceptable performance it will be a huge battery drain.
If you wish to play a video in portrait mode, I have solution for that.
If you think that -MPMovie Player can run under a view, according to me it's not possible.
MP Movie player will work as Apple has designed.
So, MP Movie player will always / almost run in full screen.
Solution for portrait mode.
#interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
#end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
Hope that will help to you.
See, My question is very similar like yours.
playing video in custom size screen - view in iphone
Try this:
UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor orangeColor];
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(#"cannot find %# in bundle or doctuments", path);
}
NSURL *url = [NSURL fileURLWithPath:path];
MoviePlayerViewController *mpvc = [[MoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpvc.moviePlayer];
mpvc.moviePlayer.fullscreen = NO;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleNone];
mpvc.moviePlayer.view.frame = CGRectMake(10, 100, 300, 300);
[v.view addSubview:mpvc.moviePlayer.view];
[mpvc.moviePlayer play];
[self presentModalViewController:v animated:YES];
[v release];
Maybe you should check the MediaPlayer's private headers, and simply add the video's view as your view's subview.