Can you force a MPMoviePlayerPlaybackDidFinishNotification? - iphone

I have several movies that are played and presented using this code.
As you can see I also have removed the default movie controls and have added a custom overlay which essentially just stops the video.
Here is my problem... When I stop the movie with my custom overlay button, I don't seem to be getting the 'MPMoviePlayerPlaybackDidFinishNotification'
Note: everything works normal if I let the movie play through and it stop by itself.
Is the any way of 'forcing' the PlaybackDidFinish notification?
Can I do something like this [self moviePlayBackDidFinish:something]; ?
Thank You!
- (void) playMovie {
NSString *path = [[NSBundle mainBundle] pathForResource:#"movie_frog" ofType:#"m4v"];
NSURL *url = [NSURL fileURLWithPath:path];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(mp) {
self.myMoviePlayer = mp;
[mp release];
//movie view
[self.view addSubview:myMoviePlayer.view];
myMoviePlayer.view.frame = CGRectMake(0.0,0.0,480,320);
self.myMoviePlayer.controlStyle = MPMovieControlStyleNone;
[self.myMoviePlayer play];
//videoNav
_videoNav = [[videoNav alloc] initWithNibName:#"videoNav" bundle:nil];
[self.view addSubview:_videoNav.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
}

I use
[[NSNotificationCenter defaultCenter] postNotificationName: MPMoviePlayerPlaybackDidFinishNotification object:nil];

Related

streaming videos on iPhone

I want to stream large videos from ftp in iPhone. The video are in size more then 500 MB. I have never done streaming so have no idea about it. I have checked live streaming guide from Apple but it does not provide any help regarding coding in iPhone. Can some one help me what exactly I have to do in iPhone coding? So far I have done following:
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:#"http://www.defencecourse.com/digital-reproductions/yellow-belt.mp4"]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
[mpvc release];
Is this coding enough to play a streaming video?
I have a guy who prepare videos for me what should I exactly ask him to do with videos on the server? Should I ask him just to split videos on server or something else?
Can Someone please suggest me best way to forward?
Regards
Pankaj
Check these tutorials AVFoundation Tutorials and read out the Apple's AVFoundation Framework programming guide here
AVFoundation Framework is much more powerful.
Hi You have to do following things on iphone side ...
-(void) btnClose_clicked {
[appDelegate.navShowController dismissModalViewControllerAnimated:YES];
}
-(IBAction) btnPlay_clicked {
// NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"3idiots.mov" ofType:nil];
// NSURL *url =[NSURL fileURLWithPath:urlStr];
NSURL *url = [[NSURL alloc] initWithString:[self.DiscAnsDetail objectForKey:#"product_video"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
// Use the new 3.2 style API
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
} else {
// Use the old 2.0 style API
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([moviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[moviePlayer.view removeFromSuperview];
}
[moviePlayer release];
}

MPMoviePlayerViewController quits while playing the video

I am recording the video in my application. I am saving that video inside Documents directory. After taking the video if I try to play the video using the following code. The player opens and quits in one seconds. However if I Quit and then open my app fresh the video I took earlier is playing.
+ (void) playMovieAtURL: (NSURL*) theURL :(id)sender{
NSLog(#"playMovieAtURL");
//senderID = (id *)sender;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[sender presentMoviePlayerViewControllerAnimated:mp];
//mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mp.moviePlayer play];
}
[mp release];
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
[theMovie release];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *aMoviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:aMoviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([aMoviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[aMoviePlayer.view removeFromSuperview];
}
}
This is the code I use for storing the video inside documents.
NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
self.itsVideoName = fileName;
[webData writeToFile:[NSString stringWithFormat:#"%#/%#",dataPath,fileName] atomically:TRUE];
I finally figured out the answer after few hours of searching. This is the code I am using now..
-(IBAction)playMovie:(NSString *) theURL
{
NSURL *fileURL = [NSURL fileURLWithPath:theURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
//After putting the following line the problem solved.
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
the code moviePlayerController.useApplicationAudioSession = NO; is used to solve this problem. I am not sure how this problem solved I guess I am using session for audio I guess that was causing problem to this.
Also the code which I originally posted was taking lots of memory like if I opened 2 video it cost 104 mb allocation for me. This new code was perfect and it is taking only less memory. Hope this will help for some one..
*NSURL fileURL = [NSURL fileURLWithPath:theURL];
should use NSURL *fileURL = [NSURL URLWithString:theURL];

Video won't play on iPhone 5.0 simulator

Im new to xCode programming, ive got this code from an ebook tutorial playing mp4 xCode.
function triggered from a button
(IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"videoSample" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen=YES;
[moviePlayerController play];
moviePlayerController.scalingMode = MPMovieScalingModeFill;
}
called from play movie function
(void)moviePlaybackComplete:(NSNotification *)notification{
MPMoviePlayerController *moviePlayerController = [notification
object];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}
Ive got no error and warning after i build it. it just output a blank screen after i click the button that triggers the playMovie function. Im confuse ive google it and still got no idea on how to solve the proble.
Im using xCode 4.2 iOS SDK 5.0
try this....replace "example" with the name of your file and "m4v" with the type of file e.g. "mp4" (not .mp4 and dont put the .mp4 bit in the name of the file)
-(IBAction)playVideo:(id)sender;
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"m4v"];
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
if (tmpMoviePlayViewController) {
tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tmpMoviePlayViewController animated:YES];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)theNotification
{
MPMoviePlayerController *moviePlayer=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
I don't know if you are still looking for the answer. Here's what you can try - declare the MPMoviePlayerController in your header(.h) file (i.e. make it a member variable).
MPMoviePlayerController *moviePlayerController;
and add this to your .m file
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
If it still does not work then try to declare it as a #property.

Video Clip does not play correctly

I am trying to play a video in one of my screens. It seems to run but when the file opens, the fullscreen loads up and closes right after. The video is a few seconds long, and is m4v format. I added the framework and imported the class. How can I make it so that the video plays properly?
My header:
-(IBAction)playMedia:(id)sender {
NSString *movieFile;
MPMoviePlayerController *moviePlayer;
movieFile = [[NSBundle mainBundle]
pathForResource:#"movie" ofType:#"m4v"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath: movieFile]];
[moviePlayer.view setFrame:CGRectMake(145.0, 20.0, 155.0 , 100.0)];
[self.view addSubview:moviePlayer.view ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
if ([toggleFullscreen isOn]) {
[moviePlayer setFullscreen:YES animated:YES];
}
}
My implementation file is:
-(void)playMediaFinished: (NSNotification*)theNotfication {
MPMoviePlayerController *moviePlayer=[theNotfication object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
thanks

How to change view after video played?

I would like to make an application in which, when I press a button, one video starts playing, and when it finishes or when I press "done" button it should take me to a view different from the first one where I launched the video.
Update:
This is the code I'm using but it doesn't work. I need to use MPMoviePlayerViewController instead of MPMoviePlayerController. Any idea?
NSBundle *Bundle = [NSBundle mainBundle];
NSString *moviePath = [Bundle pathForResource:#"video1" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerViewController *moviePlayer = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer release];
// dismiss your view or present a new view here.
View1 *View1b = [[View1 alloc] initWithNibName:nil bundle:nil];
View1b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController: View1b animated:YES];
}
Unless you need something very custom, MPMoviePlayerController will probably suit your needs. You can add it to your view or a smaller subview, and you can disable the controls for fullscreen, etc. The url can be to a local file or remove resource.
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player view].frame = [myView bounds];
[myView addSubview: [player view]];
[player play];
Observe the MPMoviePlayerPlaybackDidFinishNotification to figure out when it is done, and from that observer's block or selected method you can dismiss your view or present a new view.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
Then
- (void) movieFinishedCallback:(NSNotification*) notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player release];
// dismiss your view or present a new view here.
}