6
votes

I'm trying to play a YouTube video within an iPhone app using the technique in this URL http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application

The technique works fine and the video plays fine, except that i want the video to switch back to the main view when its finished. when i click on "done" button of the youtube player it does turn back to the main view but when the video is finished the youtube player doesn't disappear(you have to click "done")

Please help!

2

2 Answers

0
votes
 NSURL *url = [[NSURL alloc]initWithString:@"url for video"];   
        self.backgroundColor=[UIColor redColor];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
        [moviePlayer.view setFrame:CGRectMake(0,0,self.frame.size.width-10,self.frame.size.height-10)];
        moviePlayer.controlStyle =  MPMovieControlStyleNone; 
        moviePlayer.shouldAutoplay=YES;
        moviePlayer.repeatMode = NO;  
        [moviePlayer setFullscreen:YES animated:YES];  
        [moviePlayer prepareToPlay];
        pause=NO;
        [self.view addSubview:moviePlayer.view];
0
votes

You can use notification to inform when its finished so that you can remove it from the view and let previous view on screen.

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

Don't forget to remove notification in moviePlaybackComplete: by

MPMoviePlayerController *moviePlayerController = [notification object];  
[[NSNotificationCenter defaultCenter] removeObserver:self  
                                                name:MPMoviePlayerPlaybackDidFinishNotification  
                                              object:moviePlayerController];