Can't run video file with MPMoviePlayerController? - objective-c

Here is my code
-(IBAction)playvideo:(id)sender
{
NSLog(#"Get in");
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"video3" ofType:#"MOV"];
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
NSLog(#"movie path %#",moviePath);
NSLog(#"movie %#",movie);
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie];
movie.controlStyle = MPMovieControlStyleDefault;
movie.shouldAutoplay = YES;
[self.view addSubview:movie.view];
[movie setFullscreen:YES animated:YES];
[movie play];
}
and Here is my log
2012-03-16 16:54:39.080 TestVideofile[510:11f03] Get in 2012-03-16
16:54:39.303 TestVideofile[510:11f03] movie path
/Users/crazyoxy/Library/Application Support/iPhone
Simulator/5.1/Applications/7F1425B8-6DB0-4A09-AC8F-3BCDBF19ED95/TestVideofile.app/video3.MOV
2012-03-16 16:54:39.304 TestVideofile[510:11f03] movie <
MPMoviePlayerController: 0x7a8ea30 >
After I click button that link with this IBAction, it turns to black page but didn't show video or even any audio.
Could you help me please?

You are using ARC and the player gets released if no further references exist.
Adding it as a property to your ViewController instance keeps the instance alive as long as the ViewController is.

Related

Make MPMoviePlayerController main view

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];

Can not detect MPMoviePlayerDidExitFullscreenNotification

In my project, I used MPMoviePlayerController to stream video from an http url. It plays fullscreen. When the video is playing, if you tap on "Done" button the video stops and it disappears, but the problem is; if you pinch to close video screen the video screen disappears but it still plays, sound of video continues to play.
I tried to detect exit fullscreen notification and manually stop the video but it didn't work. My moviePlayerDidExitFullScreen method didn't called.
To control that if I am getting the notifications on the right way I tried to get another notification : MPMoviePlayerPlaybackStateDidChangeNotification, and it is working. It calls the method on video launching.
I searched many forums and Apple documentations but I couldn't find enough information.
Here is my code to open a fullscreen video and detect exit fullscreen :
- (void)openFullVideo
{
NSString* path = #"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4";
NSURL *fileURL = [NSURL URLWithString:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
player.controlStyle = MPMovieControlStyleDefault;
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:player.view];
[player setFullscreen:YES animated:YES];
[player play];
}
- (void) moviePlayerDidExitFullScreen:(id)sender {
NSLog(#"moviePlayerDidExitFullScreen");
}
OK, I played with your code for a while, and finally shot that little bug in the gut.
Your first problem is not retaining the player object (assuming you are using ARC, if not, then skip this). So, just make sure you retain it as an instance variable for example:
//Header File
#interface ViewController : UIViewController {
MPMoviePlayerController* _player;
}
// Implementation File
- (void)openFullVideo {
// ...
_player = player;
}
Now, if that just works, then great!! But I am getting a dreaded unsolved bug on apple's side:
An AVPlayerItem can occupy only one position in a player's queue at a time
To solve this issue, do it like so:
NSString* path = #"http://trtvizyon.mysys.com/test/leyla_ile_mecnun.mp4";
NSURL *fileURL = [NSURL URLWithString:path];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
player.controlStyle = MPMovieControlStyleDefault;
player.movieSourceType = MPMovieSourceTypeStreaming;
[self.view addSubview:player.view];
[player setContentURL:fileURL];
[player setInitialPlaybackTime:-1.f];
[player setFullscreen:YES animated:YES];
[player prepareToPlay];
[player play];
_player = player;
That should do it!
Some Other Friendly Advice:
Make sure you remove yourself from NSNotificationCenter before playing the movie again.
I would suggest adding something like if (_player != nil) to avoid recreating the object.

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.