Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPMoviePlayerController gives me a black empty view,no video playing

iOS 5.1 ,here is the code:

 MPMoviePlayerController *player =
    [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
    [player prepareToPlay];
    [player.view setFrame: self.view.bounds];  // player's frame must match parent's
    [self.view addSubview: player.view];
    // ...
    [player play];

in fact ,this is just the same as the apple reference say,but when I click the button to use this function,there is just a black Rectangle stay there,no vedio playing ,no sound happening,and no crush,so I want to know how to make this work

like image 370
Pengxuan Li Avatar asked May 03 '12 07:05

Pengxuan Li


3 Answers

Sorry for very late reply. The Solution is kind of wierd. But it helped me keep going while facing the same issue.

MPMovieSourceTypeStreaming is the thing you have missed.

MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString:@"map.mp4"]];
[player prepareToPlay];
[player.view setFrame: self.view.bounds];  // player's frame must match parent's
player.movieSourceType    = MPMovieSourceTypeStreaming;
[self.view addSubview: player.view];
// ...
[player play];
like image 135
Suresh Varma Avatar answered Nov 03 '22 02:11

Suresh Varma


You may be sending your

[player play] 

message too soon.

Use the following notification observation

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMovie:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];

To listen for the loadState to change to playable. To check for that update do this :

- (void)playMovie:(NSNotification *)notification {
    MPMoviePlayerController *player = notification.object;
    if (player.loadState & MPMovieLoadStatePlayable)
    {
        NSLog(@"Movie is Ready to Play");
        [player play];
    }
}

The movie should begin playing once it's ready.

like image 21
David Hegner Avatar answered Nov 03 '22 01:11

David Hegner


Try this below code for play video from your project Resource :

  NSBundle *bundle = [NSBundle mainBundle];
  NSString *moviePath = [bundle pathForResource:@"map" ofType:@"mp4"];
  NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];

  MPMoviePlayerController *player =
  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
  [player prepareToPlay];
  [player.view setFrame: self.view.bounds];  // player's frame must match parent's
  [self.view addSubview: player.view];
    // ...
  [player play];

Thanks..!

like image 21
Dinesh Avatar answered Nov 03 '22 02:11

Dinesh