开发者

not able to release MPMoviePlayerController

I created a global variable:

MPMoviePlayerController *player;

I play the video with the following method:

-(IBAction) playMovie: (NSString*) videoName ViedeoType:(NSString*) videoType{

ViewVideoSubview.alpha = 0;

NSString *url = [[N开发者_开发问答SBundle mainBundle] 
                 pathForResource:videoName 
                 ofType:videoType];


player = 
[[MPMoviePlayerController alloc] 
 initWithContentURL:[NSURL fileURLWithPath:url]];


player.shouldAutoplay =YES;



[ViewVideoSubview addSubview:player.view];



[[NSNotificationCenter defaultCenter] 
 addObserver:self
 selector:@selector(movieFinishedCallback:)                                                 
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:player];

}

and when the video finishes playing by itself the folloing method get's called:

- (void) movieFinishedCallback:(NSNotification*) aNotification {



    [player.view removeFromSuperview];  //d1
    MPMoviePlayerController *playerParam = [aNotification object];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:playerParam];  

     [player release];

}

Everything works great so far. The problem is that I have a button that when pressed I need to load another view controller. I am able to load that view controller but the video still plays in the background. I don't why I get an error when releasing the player. my temporary solution is to stop the video then load the other view controller so that the video does not play in the background.

Another solution that I was thinking of is to play the video 1 second before it finishes playing so that it gets released with the method movieFinishedCallback. I don't know how I will be able to "fast forward" the video to that point. I am new to objective-c and I don't know what is the aNotification parameter otherwise I will just call that method with the appropriate parameter.

Let me show you the error that I am getting:

not able to release MPMoviePlayerController

not able to release MPMoviePlayerController


I think your problem lies in the way you are trying to remove an observer in the method movieFinishedCallback

here you are passing a pointer to your global property player.

MPMoviePlayerController *playerParam = [aNotification object];

and here you are invocing a method to remove observer for all notifications regarding this object playerParam

[[NSNotificationCenter defaultCenter] 
 removeObserver:self
 name:MPMoviePlayerPlaybackDidFinishNotification
 object:playerParam];  

Now you get an EXEC_BAD_ACCESS because you are sending a pointer (playerParam) to your player (already released somewhere) to a method (removeObserver) causing an operation of removeObserver to be called on an non-existing object.

Instead of using

[[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:playerParam]; 

try

[[NSNotificationCenter defaultCenter] 
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:nil]; 

Making your object nil will:

-(void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender

notificationSender ... When nil, the receiver does not use notification senders as criteria for removal.

More info can be found in NSNotificationCenter Class Reference

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜