Search code examples
iosobjective-cxcodempmovieplayercontroller

launching video at application launch fails iOS


I want to show a small video (3 sec video) every time when user opens the application on iOS device. (If the app is opened from memory then don't show video)

I am having my "dashboard" view as root view controller.

I searched all the links on SO and other sites, but none helps.

Sometimes my video is working but later application get crashed.

My dashboardview is from "Main.storyboard" but there are no controls on storyboard. Only linking of navigation bar is done via storyboard.

Here is my code.

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


- (void)showVideo
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSLog(@"video path :- %@",url);
    self.navigationController.navigationBar.hidden = YES;
    self.videoController = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    self.videoController.moviePlayer.controlStyle = MPMovieControlStyleNone;
    //[self presentMoviePlayerViewControllerAnimated:videoController];
    [self.navigationController pushViewController:self.videoController animated:NO];

    [self.videoController.moviePlayer play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.videoController];
}

- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
    self.navigationController.navigationBar.hidden = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];

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

    [self.navigationController popViewControllerAnimated:NO];
    //[self.view removeFromSuperview];
}

the nslog is printing video path correctly. like

video path :- file:///Users/itshastra/Library/Developer/CoreSimulator/Devices/87C93694-66E8-4884-B087-10E1E4CBA4D1/data/Containers/Bundle/Application/DB6C89D4-EE6D-4830-B208-B4AA89FD8E59/Complaint.app/video.mp4

But App get crashed, Neither it shows the video and then dashboard, not dashboard directly.

Can any one please guide me, what I am doing wrong.

Thanks in advance.

If by mistake I missed any link on SO, please provide that as well. Thanks.


Solution

  • Try calling method like this,

    - (void)viewDidLoad
    {
        [super viewDidLoad];        
        [self performSelector:@selector(showVideo) withObject:nil afterDelay:0.1f]; 
    }