Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

moviePlayer - Black screen appears after finished playing

My issue is that when I press the done button on my movie or the movie finish playing a black screen appears. I would like to get back to the start screen where I placed a UIImage as a start screen or that the movie pushes back to settings. Both alternatives would be good for me. Any ideas?

-(void)playMovie:(id)sender
{

    UIButton *buttonThatWasPressed = (UIButton *)sender;
    buttonThatWasPressed.enabled = NO;

    NSString * str=[[NSBundle mainBundle]pathForResource:@"yo2" ofType:@"mov"];
    NSURL * url=[NSURL fileURLWithPath:str];
    MPMoviePlayerController * movieController=[[MPMoviePlayerController alloc]initWithContentURL:url];
    movieController.controlStyle=MPMovieControlStyleFullscreen;
    [movieController.view setFrame:self.view.bounds];
    [self.view addSubview:movieController.view];
    [movieController prepareToPlay];
    [movieController play];
    _moviePlayer =  [[MPMoviePlayerController alloc]
                     initWithContentURL:url];


    _moviePlayer.controlStyle = MPMovieControlStyleDefault;

    _moviePlayer.shouldAutoplay = YES;
    [self.view addSubview:_moviePlayer.view];
    [_moviePlayer setFullscreen:YES animated:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:_moviePlayer];


}



- (void) moviePlayBackDidFinish:(NSNotification*)notification {

    // Remove observer
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 [self performSegueWithIdentifier:@"back" sender:self];
                             }];

}

@end

1 Answer

Maybe push to rootview instead?