Make MPMoviePlayerController main view - objective-c

I have got a horizontal scrollview like the Springboard with 16 buttons on each of these pages. When you click a button I want to only see the MPMoviePlayerController which plays a video and nothing else, like if you are watching a movie in the videos application. Therefore, I've got the following code:
- (void)button: (id) sender {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"video" ofType:#"MOV"];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
moviePlayerController.fullscreen = YES;
[self.view addSubview:moviePlayerController.view];
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
Unfortunately, I only get to hear sound and there is no video showing. Can anybody help me, please?
Update: I found out that you would have to make a frame. I used the following code:
[moviePlayerController.view setFrame:CGRectMake(0, 0, 768, 1004)];
But it isn't in fullscreen that way. Even if I've set the fullscreen property to YES. I can see that because there are no video-controls at the top of the view but at the bottom of the view.

I found the solution. You should use the following code:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"1" ofType:#"MOV"];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentModalViewController:playerView animated:YES];

Related

MPMoviePlayerController to full screen

I am placing an MPMoviePlayerController in my view at a certain size and position. This is working fine. However, in certain circumstances, I want the video to play full screen. My view is part of a UISPlitViewController, so if I just get the view width, it is not full screen. What I need is to show the video as if the user had clicked the double ended arrow in the player controls to maximise the video.
Here is my code so far. Can anyone fill in the missing bit that forces the full screen playing?
- (void)viewDidLoad
{
[super viewDidLoad];
// FIND OUT HOW MANY VIDEOS ARE AVAILABLE
int videoCount = [[self videos] count];
// GET THE FILE NAME OF THE FIRST AVAILABLE VIDEO
NSString* fileName = [NSString stringWithFormat:#"%#.mp4", [self videos] objectAtIndex:0]];
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL fileURLWithPath:filePath];
// PLACE THE MOVIE AT THE CORRECT LOCATION ON THE PAGE
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[moviePlayer view] setFrame:CGRectMake(100, 100, 600, 360)];
[[moviePlayer view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[[self view] addSubview:moviePlayer.view];
[moviePlayer play];
if(videoCount == 1)
{
-- MAXIMISE THE VIDEO TO FULL SCREEN AND LANDSCAPE --
}
}
Thanks
As per Apple's documentation, you just have to set the `MPMoviePlayerController to go full screen:
[moviePlayer setFullscreen:YES animated:YES]
Have you tried this?
Please use MPMoviePlayerViewController Because of MP4 file. When you use MOV then working perfect!!
MPMoviePlayerViewController *moviePlayerViewController;
-(void)PlayVedioController:(NSString*)videoUrl1
{
NSURL *fileURL = [NSURL URLWithString:videoUrl1];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
// Register for the playback finished notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerViewController.moviePlayer];
//Present
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
// Play the movie!
moviePlayerViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayerViewController.moviePlayer play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerViewController.moviePlayer];
[moviePlayerViewController release], moviePlayerViewController = nil;
}

MPMoviePlayerController stops after four seconds

I am trying to set up a very simple video player. (iOS 5.1, Xcode 4.3.1)
-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];
[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self.view addSubview: moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
}
It works fine when called, but it only plays for four seconds, then a black screen appears. If I tap the screen during playback, it will play the entire sequence. If I stop tapping the screen for four seconds, the black screen appears.
What am i missing?
Kurt
Edited version plays fine.
In the interface file:
#property (nonatomic,strong) MPMoviePlayerController *myMovieController;
In the .m file:
-(void)playMedia {
NSString *movieFile = [[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"m4v"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];
[moviePlayer prepareToPlay];
moviePlayer.view.frame = self.view.bounds;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.fullscreen = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
self.myMovieController = moviePlayer;
[self.view addSubview: self.myMovieController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[self.myMovieController play];
}
If you're using ARC I believe you need to retain the outer moviePlayer. I just assigned it to a new property myself. HTH
The solution is that the player would have to be an instance variable or property of the view controller.
ie We must use the instance of MPMoviePlayerController
#property (nonatomic,strong) MPMoviePlayerController *myMovieController;

When my movie is finished to play, the app crashes

I tried to launch a movie by clicking in a button. When I click the Movie player appears and works fine. The movie plays until the end and the Movie player disappears. After it disappears, my app crashes...
I use my View Controller in Tab Bar Application with this code :
- (void)moviePlayBackDidFinish:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[player setFullscreen:NO animated:YES];
[player.view removeFromSuperview];
[player stop];
player.initialPlaybackTime = -1.0;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player release];
player=nil;
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
player.shouldAutoplay = YES;
player.view.frame = CGRectMake(0., 44., self.view.bounds.size.width, self.view.bounds.size.height-44);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[self.view addSubview:player.view];
[player play];
}
I don't know what I'm doing wrong. I just want configure a button that start the video and when the video is finished, the MoviePlayer View disappears and the app come back to my initial .xib
Thank you for your help
I found the solution, I don't know if it is the best but for what I want it's working :
- (void)movieFinishedCallBack:(NSNotification *) aNotification{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player.view removeFromSuperview];
[player stop];
[player release];
}
- (IBAction)playVideo:(UIButton *)playButton{
NSString *url = [[NSBundle mainBundle] pathForResource:#"Teaser 04" ofType:#"mov"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
player.view.frame = CGRectMake(0, 0, 950, 600);
[self.view addSubview:player.view];
[player play];
}
I could be wrong, but my guess is that the player gets used somewhere in the backtrace for the call to moviePlayBackDidFinish: ... If I'm right, then deallocing the object there - which it looks like you're doing - is bad news, since the object is still needed somewhere up in the stack.
Check out this post for more details: How to release MPMoviePlayerController?

Problem with video on NavigationController + TabBarController

I have an application with a TabBarController containig two TabBars with a NavigationController.
In the first tabbar, I have a ViewController containing some buttons. One of these buttons enable the user to play video. And all run fine for the moment.
My problem is that when the video is being played and I move to the second TabBar, the video continue playing (I hear the sound), then if I come back to the first TabBar (where there is my buttons) the application crashes without any notification.
I don't understand if it's a memory problem or something else.
Any suggestions?
Here is the function used :
-(IBAction)playMovie:(id)sender
{
videoView = [[UIViewController alloc]initWithNibName:#"video" bundle:nil];
videoView.title = #"title";
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"video.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
testbtnAppDelegate *appDelegate = (testbtnAppDelegate *)[[UIApplication sharedApplication] delegate];
videoView.view=moviePlayerController.view;
[moviePlayerController play];
[appDelegate.navController pushViewController:videoView animated:YES];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
[moviePlayerController pause];
before u go to secondtab pause.

Playing video and audio in iPhone not working

So we have buttons linked up to display images/videos/audio on click depending on a check we do earlier. That part works fine. It knows which one to play, however, when we click the buttons for video and audio, nothing happens. The image one works fine.
The video and audio are being taken for a URL online, they are not local, but everywhere said this was still possible. Here is a little snippet of the code where we play the two files:
if ( [fName hasSuffix:#".png"])
{
NSLog(#"PICTURE");
NSURL *url = [NSURL URLWithString:
fName];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"MainBG.jpg"]];
[self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}
if ( [fName hasSuffix:#".mp4"])
{
NSLog(#"VIDEO");
//NSString *path = [[NSBundle mainBundle] pathForResource:fName ofType:#"mp4"];
//NSLog(path);
NSURL *url = [NSURL fileURLWithPath:fName];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player play];
}
if ( [fName hasSuffix:#".mp3"])
{
NSLog(#"AUDIO");
NSURL *url = [NSURL fileURLWithPath:fName];
NSData *soundData = [NSData dataWithContentsOfURL:url];
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithData:soundData error: nil];
[avPlayer play];
}
See anything wrong? By the way it compiles and runs, but nothing happens when we hit the button that executes that code.
I am not sure why they are not playing, does it ever reach the video and audio conditons?
Either way your currently missing before you play the movie
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
You will then need the function to handle the callback
-(void)movieFinishedCallback:(NSNotification *)notification{
NSLog(#"MOVIE FINISHED");
if(player == [notification object]){
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player release];
player = nil;
}
}