Search code examples
iosobjective-cipadmpmovieplayercontrolleruisplitviewcontroller

MPMoviePlayerController won't open


I'm making an app with a splitviewcontroller and want to play an video.

I have 2 controllers in the split view. Left (Master) is VideoMenuTableViewController Right (detail) is VideoViewController

First i tested if the video works by putting my code in the viewdidload of VideoViewController.

Like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
self.URLForVideoFile = @"http://api.smit-it.info/TEST/VIDEO/two.mov";
    NSURL *fileURL = [NSURL URLWithString:self.URLForVideoFile];

    MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];

    [self presentMoviePlayerViewControllerAnimated:mp];

}

This works and the video plays.

Now i try to open the video by touching the title in the VideoMenuTableViewController.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *SelectedUrl;
    SelectedUrl = [[[self.dataSource.videos valueForKey:@"URL"] objectAtIndex:0] objectAtIndex:indexPath.row];

    NSLog(@"URL pressed %@",SelectedUrl);

    VideosViewController *vvc = [[VideosViewController alloc] init];
    vvc.URLForVideoFile = SelectedUrl;

    [vvc PlayMovieFromSelectedUrl];

}

Where PlayMovieFromSelectedUrl is the same code as in the viewdidload The app crashes and gives the following error.

Warning: Attempt to present <MPMoviePlayerViewController: 0xb25c440>
on <VideosViewController: 0xb2565d0> whose view is not in the window
hierarchy!

But i don't understand the problem, so i don't know how to fix it. Please help.


UPDATE

By adding 3 lines of code by @PiotrK

UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: vvc.view];
[self presentViewController:vvc animated:NO completion:nil];

The video plays but when the video is done or dismissed the split view stops responding to everything.


Solution

  • Well, there's been some time since I used iOS SDK, but I'll try to help. The error says it: your VideosViewController is not in the window hierarchy. It means that the window manager doesn't know about the existence of the vvc VideosViewController. You have to tell the application that you actually want to display vvc. Try this way:

    ...
    
    VideosViewController *vvc = [[VideosViewController alloc] init];
    vvc.URLForVideoFile = SelectedUrl;
    
    UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview: vvc.view];
    [self presentViewController:vvc animated:NO completion:nil];
    
    [vvc PlayMovieFromSelectedUrl];
    
    ....