Msmit1993
Msmit1993

Reputation: 1710

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.

Upvotes: 0

Views: 793

Answers (2)

PiotrK
PiotrK

Reputation: 1552

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];

....

Upvotes: 1

mani murugan
mani murugan

Reputation: 213

Allocate MPMoviePlayerViewController *mp in .h file..or interface in .m file

Upvotes: 0

Related Questions