Notifications don't seem to be fired from MPMoviePlayerController - iphone

I'm trying to get a simple video to play on an iPhone with iOS 4.3 and am running in to issues with dismissing the video player. Below is the code I have set up ... I'm listening for three different notification events to help signal when the movie player and its parent view should exit, but none of those events seem to get fired. My callback method is never invoked.
NSString* moviePath = [[NSBundle mainBundle] pathForResource:#"sample1" ofType:#"m4v"];
NSLog(#"Here is the movie path: %#", moviePath);
NSURL* movieUrl = [NSURL fileURLWithPath:moviePath];
NSLog(#"Will now try to play movie at the following url: %#", movieUrl);
self.mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mpvc.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:self.mpvc.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerWillExitFullscreenNotification object:self.mpvc.moviePlayer];
[[self.mpvc moviePlayer] prepareToPlay];
[[self.mpvc moviePlayer] setShouldAutoplay:YES];
[[self.mpvc moviePlayer] setFullscreen:YES];
[self.mpvc.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview:self.mpvc.view];

Are you sure your self.mpvc.moviePlayer is a valid object which is sending the notification.
Are you sure you MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerDidExitFullscreenNotification MPMoviePlayerWillExitFullscreenNotification same notification name..
Here is the sample code/example of NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(manageHistory:) name:#"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(#"Your information embedded to dictionary obj %#",_dict);
}

You probably did a mistake in registering the movie player callbacks. The object parameter should be self.mpvc instead of self.mpvc.moviePlayer. So try to replace your 3 lines with these:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.mpvc];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerDidExitFullscreenNotification object:self.mpvc];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerFinished:) name:MPMoviePlayerWillExitFullscreenNotification object:self.mpvc];

Related

Video from web cam is not streaming in iPhone/Objective c

I am trying this code but it shows a still image, and not proper video streaming. However it shows the video, but only for some time.
[reykjavikurtjorn loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://213.167.154.114/mila/_definst_/tjornin.stream/playlist.m3u8"]]];
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:#"http://www.domain.com/myVideo.mp4"];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
and the delegate
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)]) {
[player.view removeFromSuperview];
}

Issue Playing sound from url

I am making an Iphone app which have requirement to play sound in a loop. The sound will come from HTTP links like follows:
Link 1
Link 2
Link 3
Link 4
Link 5
Link 6
Link 7
Link 8
Actually there are three button loop,play all and stop. I am able to do play once click all and loop but When i click other button app will crash.How can i solve this problem.My code is below resultArrayFinal is a globle array where all above sound link store.
-(void)replayVedio
{
DataClass *obj=[DataClass getInstance];
NSURL *url = [NSURL URLWithString:obj.soundInfo];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
if(_globle==1)
{
[self replayAudio];
}
else if(_globle==2)
{
DataClass *obj=[DataClass getInstance];
obj.soundInfo=[resultArrayFinal objectAtIndex:count++];
if (count==[resultArrayFinal count])
count=0;
[self replayAudio];
}
}
Thanks.

MPMoviePlayerController not working, no Notifications either

Can someone please point out why this would NOT work?
NSURL *url = [NSURL fileURLWithPath:ImageName];
MPMoviePlayerController *video = [[MPMoviePlayerController alloc] initWithContentURL:url];
NSLog(#"%#", ImageName);
NSLog(#" %#", url);
[canvas addSubview:video.view];
video.view.frame = canvas.bounds;
// [video setControlStyle:MPMovieControlStyleEmbedded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:video];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieCallback:) name:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey object:video];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieCallback:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:video];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieCallback:) name:MPMoviePlayerLoadStateDidChangeNotification object:video];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieCallback:) name:MPMoviePlayerNowPlayingMovieDidChangeNotification object:video];
[video prepareToPlay];
None of the callbacks trigger. Just have a blank space where canvas is.
Keeping the MPMoviePlayerController reference in an instance variable or property worked for me.

MPMoviePlayer-Notification doesn´t get called

HI,
i want to play a movie using MPMoviePlayerViewController and listen to a MPMoviePlayerPlaybackDidFinishNotification, but it doesn´t get called. Any ideas on that?
Would be helpful to get any hints on that.. Thanks you
Here´s my code:
NSString *url = [[NSBundle mainBundle] pathForResource:[sender contentName] ofType:#"m4v"];
MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
}
- (void) moviePlayBackComplete:(NSNotification*) notification {
NSLog(#"moviePlayBackComplete complete");
MPMoviePlayerController* moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self dismissMoviePlayerViewControllerAnimated];
[moviePlayerController release];
}
You're trying to get notifications from a MPMovePlayerViewController but I'm pretty sure only MPMoviePlayerController provides them. Try and change your notification setup
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];

MPMoviePlayerController release problem

here is code
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie stop];
[theMovie release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
so my problem is my apps memory usage goes additional 3MB , it stays there even after release,does that mean memory does not get released?
Look at your code in
- (void) moviePlaybackDidFinish:(NSNotification*)notification
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
Are you sure "theMovie" is your created "moviePlayer"? I believe they are different memory address because you didn't assign a object when you register the notification. Make sure
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
Then try it again.