Search code examples
ioswebviewyoutubempmovieplayercontroller

ios youtube video keeps on playing in background


I am playing YouTube videos in a separate viewController (say videoViewController) in my iPad app, i have a home view controller having a button to load videoViewController.

Every thing works fine video is playing when i click on play icon. I am using this code

[_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [NSString stringWithFormat:@"https://www.youtube.com/embed/%@", @"eOG90Q8EfRo"]]]];

Now this videoViewController have a back button on the top using below code:

[self.navigationController popViewControllerAnimated:YES];

When I click it, the home viewController is displayed but the video keeps on playing. Even when I again go to videoViewController it is still playing until I select a new video to play.

I tried calling viewDidUnload before piping the view controller and tried below code

- (void)viewDidUnload {
    [_webview stopLoading];
    _webview.delegate = nil;
    [self setWebview:nil];
}

However nothing happens, I want that video to stop playing when I press the back button.

I guess the video is playing in the MPMoviePlayerController provided by the iOS is there any way so that I can get a reference to it to make the video stop.


Solution

  • Ok i found the solution to my problem.

    Finally i got it working :

    -(void)viewWillDisappear:(BOOL)animated
    {
        [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
    }