Search code examples
iosobjective-cmpmovieplayercontroller

Call MPMoviePlayerViewController in app delegate


I am using story board in app. Using storyboard all views are connected properly Now new thing I want to do is,

When my Splash screen goes down, I want to show 3 sec video every time user opens the app.

I know how to load video from viewcontroller, Following is code that I used to launch the video.

- (void)showVideo
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSLog(@"video path :- %@",url);
    videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [self presentMoviePlayerViewControllerAnimated:videoController];
    [videoController.moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:videoController];
}

- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];

    [videoController.view removeFromSuperview];
    [videoController.moviePlayer stop];
    videoController = nil;
    [self.view removeFromSuperview];
}

But when I use this code in my rootViewController app crashes saying

Attempt to present <MPMoviePlayerViewController:> on <DashbaordVC:> whose view is not in the window hierarchy

But when I use same code in other demo app using navigation controller (No Storyboard) it works fine. But in this app where story board is used, it crashes.

Also I tried

[self.navigationController pushViewController:self.videoController animated:NO];

Then I thought of adding this code in AppDelegate file and calling the method from ApplicationDidFinishLaunching

But didn't help. can anyone guide me.... for the same Also how to add MPMoviePlayerViewController in app delegate.


Solution

  • I think you can do this by MPMoviePlayerController. Try following code

             NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
             NSURL *url = [NSURL fileURLWithPath:path];
    
            videoPlayer = [[MPMoviePlayerController alloc] init];
            [videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - (viewTopbar.frame.size.height + 50.0))];
                    [videoPlayer setMovieSourceType:MPMovieSourceTypeFile];
    
            [videoPlayer setContentURL:url];
            [videoPlayer setControlStyle:MPMovieControlStyleEmbedded];
            [videoPlayer setScalingMode:MPMovieScalingModeNone];
            [videoPlayer prepareToPlay];
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(moviePlayBackDidFinish:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:videoPlayer];
    
           [self.view addSubview:videoPlayer.view];
           [videoPlayer play];
    

    And if you dont want full screen then used it like this

    [videoPlayer setControlStyle:MPMovieControlStyleNone];