Reputation: 3356
I am trying to add an intro video into my iOS app. I want the video to play with no controls and in a box I create for it. but am not sure how to do that. It is also not playing at the correct size it is super small. If you look below you will see my attempt but it fails to work. How do I achieve this?
-(void)initVideo{
MPMoviePlayerViewController *moviePlayerController=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"]]];
UIView * contain = [[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
[contain addSubview:moviePlayerController.view];
[self.view addSubview:contain];
MPMoviePlayerController *player = [moviePlayerController moviePlayer];
player.fullscreen = NO;
[player play];
}
Upvotes: 0
Views: 309
Reputation: 534885
I actually have code in my book that shows you exactly how to do this very thing:
http://www.apeth.com/iOSBook/ch28.html#_mpmovieplayercontroller
Read down to the first block of code. As you can see, we load a movie, decide on the rect within our view where we want to display it, and display it:
NSURL* m = [[NSBundle mainBundle] URLForResource:@"ElMirage"
withExtension:@"mp4"];
MPMoviePlayerController* mp =
[[MPMoviePlayerController alloc] initWithContentURL:m];
self.mpc = mp; // retain policy
self.mpc.shouldAutoplay = NO;
[self.mpc prepareToPlay];
self.mpc.view.frame = CGRectMake(10, 10, 300, 250);
self.mpc.backgroundView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.mpc.view];
Of course you will want to change those values! But that's the technique. Also you're going to want to get rid of the controls, but that's easy (read further down in the chapter).
Upvotes: 1
Reputation: 3698
Since you are using the player in a view, you don't need to use a MPMoviePlayerViewController
. Try the following code:
-(void)initVideo{
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"]]];
UIView *contain = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
moviePlayerController.view.frame = contain.bounds;
[contain addSubview:moviePlayerController.view];
[self.view addSubview:contain];
moviePlayerController.fullscreen = NO;
[moviePlayerController play];
}
Also, if you are using a navigation bar or status bar, you should take that away from the height:
CGRect f = [[UIScreen mainScreen] bounds];
f.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;
UIView *contain = [[UIView alloc] initWithFrame:f];
Upvotes: 0