Search code examples
iosmpmovieplayercontroller

NSUnknownKeyException in uiviewcontroller


I am trying to play a video file (m4v) from resource folder of my app (i.e local video). but I am not able to play the video. I referred apple's sample project for the MPMoviePlayer.

Also when i am giving background color to my movieplayercontroller object its appearing as its behind some black view and the black view ahead is showing that something is loading for few seconds but nothing get loaded and it results in black screen only.

Can someone help me out with it. please tell me what i am doing wrong.

Also, i have taken screenshot of the screen with problem so that you all can understand what i am trying to say but i dont know how to post it here. :(


Solution

  • NSString *urlString = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
    NSURL *urlObj = [NSURL fileURLWithPath:urlString];
    
    UIGraphicsBeginImageContext(CGSizeMake(1,1));
    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlObj];
    UIGraphicsEndImageContext();
    
    [player.view setBounds:self.view.bounds];
    // when playing from server source type shoud be MPMovieSourceTypeStreaming
    [player.moviePlayer setMovieSourceType:MPMovieSourceTypeStreaming]; // I was missing this line therefore video was not playing
    [player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
    
    
    [self.view addSubview:player.view];
    [player.moviePlayer play];