video Player not working in Xcode 4.2 - objective-c

`i am facing problem to develop a code for running a mp4 video in ios5 Xcode programing.
When i run app noting is display although clip2.mp4 file is in my bundle.please give me a
proper solution
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *url = [[NSBundle mainBundle]
pathForResource:#"clip2"
ofType:#"mp4"];
player =[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[player prepareToPlay];
[player play];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play movie---
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *play= [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:play];
/ / [player autorelease];
} `

It should be
player = [[MPMoviePlayerController alloc] initWithContentURL:url];
url is already an NSURL. -fileURLWithPath: is used to create an NSURL from an NSString that contains a file path (i.e. /path/to/file) rather than a normal URL (i.e. http://stackoverflow.com/)

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

Problem with video removeFromSuperview

NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"clip" ofType:#"m4v"];
NSURL *url = [NSURL fileURLWithPath:urlStr];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0, 0,320, 460);
[viewController.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
[moviePlayer setShouldAutoplay:YES];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer setMovieControlMode:MPMovieControlModeHidden];
I am playing mp4 file i need to removeFromSuperview when movie is finished.... in notification. is it possible.
Yes it is possible. You need to add Observers to the notifications that the MPMoviePlayer sends (probably in your viewDidLoad):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(exitedFullscreen)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(finishedPlayback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
then in your methods that are called when the notification fires:
- (void)finishedPlayback:(NSNotification *)notification
{
[self.moviePlayer setFullscreen:NO animated:YES];
}
-(void)exitedFullscreen
{
[self.moviePlayer.view removeFromSuperView];
}

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.

MPMoviePlayerController problem at start

I have a problem with MPMoviePlayerController, because when I start it in fullscreen it exists, but the song is still playing. I added the MPMoviePlayerDidExitFullscreenNotification notification and it says that when the video starts playin it exists the full screen. Here's my code:
_multimediaPlayer = [[MPMoviePlayerController alloc] init];
_multimediaPlayer.controlStyle = MPMovieControlStyleDefault;
_multimediaPlayer.initialPlaybackTime = 0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerDidExitFullscreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(videoPlayerPlaybackStateChanged:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
NSString *path = [NSString stringWithFormat:#"%#/mag_%d/%#", [FMUtils documentsFolderPathWithFile:nil], _magID, _pageObject.fileName];
if ([FMUtils fileExistsAtPath:path]) {
_multimediaPlayer.contentURL = [NSURL fileURLWithPath:path];
} else {
_multimediaPlayer.contentURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", self.dataURL, _pageObject.fileName]];
}
CGSize objectViewSize = self.frame.size;
_multimediaPlayer.view.frame = CGRectMake(0, 0, objectViewSize.width, objectViewSize.height);
[self addSubview:_multimediaPlayer.view];
if (_pageObject.blink) {
[_multimediaPlayer setFullscreen:YES animated:YES];
}
[_multimediaPlayer play];
Most of the time it happens when the video is not downloaded and it needs to stream it.
Do you have any idea why is this happen?
Problem solved while I not listen to MPMoviePlayerPlaybackStateDidChangeNotification.