MPMoviePlayerController stops after four seconds - objective-c

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;

Related

Background Video Objective-c

I want to play a loop background video in my view. I wrote this code :
-(AVPlayerLayer*)playerLayer{
if(!_playerLayer){
// find movie file
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"fond_login" ofType:#"mp4"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:[[AVPlayer alloc]initWithURL:movieURL]];
_playerLayer.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
[_playerLayer.player play];
return _playerLayer;
}
return nil; }
-(void)replayMovie:(NSNotification *)notification {
NSLog(#"Finis");
[self.playerLayer.player play];}
And in my viewdidload, I have :
[self.view.layer addSublayer:self.playerLayer];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(replayMovie:)
name: AVPlayerItemDidPlayToEndTimeNotification
object:nil];
My problem is that the video don't play in loop. I see the video one time and it's finish. I don't know why ..
Thank's
EDIT :
I see another tutorial.
#import MediaPlayer;
#property (nonatomic, strong) MPMoviePlayerController *moviePlayer;
My code in viewDidLoad :
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"my_video" withExtension:#"format"];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer.view.frame = self.view.frame;[self.view insertSubview:self.moviePlayer.view atIndex:0];
[self.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loopVideo) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
And :
- (void)loopVideo {
[self.moviePlayer play]; }
It's work perfectly.
In your replayMovie: method you need to seek back to the beginning:
- (void)replayMovie:(NSNotification *)notification {
[(AVPlayerItem *)notification.object seekToTime:kCMTimeZero];
}
You're also going to want to set the actionAtItemEnd of the AVPlayer so it doesn't pause:
_playerLayer.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;

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't run video file with MPMoviePlayerController?

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.

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.