开发者

MPMoviePlayer does not disappear on [moviePlayer.view removeFromSuperview] and [moviePlayer release]

I have a problem with MPMoviePlayerController.When I am watching a video, and hit the 'Done' button on the left top, the MoviePlayer does not disappear, even though the code seems to be called:

   NSURL *url = [NSURL URLWithString:article.enclosureLink];    

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  

    // Set movie player layout
    [moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [moviePlayer setFullscreen:YES];

    // May help to reduce latency
    [moviePlayer prepareToPlay];

    // Register to receive a notification when the movie has finished playing.      
    [[NSNotificationCenter defaultCenter] addObserver:self  
                                             selector:@selector(moviePlayBackDidFinish:)  
                                                 name:MPMoviePlayerPlaybackDidFinishNotification  
                                               object:moviePlayer];  

    [[NSNotificationCenter defaultCenter] addObserver:self  
                                                 selector:@selector(movieReadyToPlay:)  
                                                     name:MPMoviePlayerLoadStateDidChangeNotification  
                                                   object:moviePlayer];

And the selectors:

- (void) movieReadyToPlay:(NSNotification*)notification {
    MPMoviePlayerController *moviePlayer = [notification object];  

    if(moviePlayer.loadState == MPMovieLoadStatePlayable){
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];         
        moviePlayer.controlStyle = MPMovieControlStyleFullscreen;  
        //moviePlayer.shouldAutoplay = YES; 
        [self.view addSubview:moviePlayer.view];  
        [moviePlayer setFullscreen:YES animated:YES];  
        [moviePlayer play];
    }

}

- (void) moviePlayBackDidFinish:(NSNotification*)notification {  
    MPMoviePlayerController *moviePlayer = [notification object];  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];  

    [moviePlayer setFullscreen:NO animated:YES];  
    [开发者_如何学CmoviePlayer.view removeFromSuperview];  
    [moviePlayer release];

    NSLog(@"Finished movie!");
} 

This looks to me a very straight forward code, but I must make a stupid mistake. The NSLog shows that the function is called, but the player stays where it is and there is no way of getting rid of it.

Also, the very fact that the player is still operational after the alleged release seems to indicate that there is something fundamental wrong, I just don't see what.

Is there anybody who has a suggestion?

[Update:] Strangely in the iPhone Simulator it works fine!

[Update2:] I tried and created a specific UIviewcontroller, even though it is not the way I want to do it as the animations are not nice. But what I learned is that I have the same problem. It seems to hav to do something with dismissing the player, but it starting again.

When I put [self.moviePlayer setFullscreen:YES animated:YES]; in viewDidApear, and click the 'Done' button in the player, the player, the video starts over again when I hit the Done button (the viewDidAppear is called again). So something is triggered, so it seems to me, to make the video start again.

If I put it viewDidLoad, then the system works, but the graphics are mixed and confused...

Any help is really, really appreciated as I spend two days on this now without making head or tail of it!


For me, I tried all of these: [moviePlayer stop]; [moviePlayer setContentURL:nil]; [moviePlayer.view removeFromSuperview]; moviePlayer = nil;

And nothing worked. I figured out it had to due with my MPMoviePlayerController entering full screen. The fix?

        [moviePlayer setFullscreen:NO animated:YES];


Adding

[moviePlayer stop]  

before

 [moviePlayer.view removeFromSuperview]

may work.

Update: If this doesn't work then try setting controlstyle to MPMovieControlStyleNone before removing the subview.Most of the time the controlStyle causes such problems.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜