Poonam
Poonam

Reputation: 214

MPMoviePlayerController not working properly

I am making an application in which i am trying to play a video. The video starts properly but the video screen changes in black color after 4 sec. I dont know what is the problem.

also when i am setting player.movieplayer.shouldautoplay = NO, there is no effect of this line, video starts automatically.

Here is Code:

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];
[player.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];

player.moviePlayer.shouldAutoplay =  NO;

[self.view addSubview:player.view];
[player.moviePlayer play];

Am i missing something here??

I tried to get the total duration of video (using duration property of mpmovieplayercontroller) but its showing 0.0. how to get the duration of video??

Upvotes: 0

Views: 1131

Answers (2)

Martin Kenny
Martin Kenny

Reputation: 2488

There are several issues here:

  1. For this type of usage (integrating the player into your view), you should be using MPMoviePlayerController, not MPMoviePlayerViewController. Use MPMoviePlayerViewController when you want to have a self-contained view controller which can be presented using presentMoviePlayerViewControllerAnimated:.

  2. Assuming you are using ARC, the main problem is that nothing is keeping a reference to your player object. As a consequence, the player is disappearing shortly after you create it. You should keep a reference to it by assigning it to a property or instance variable of your view controller.

    For a full example of this, see Till's excellent answer to a similar question.

  3. I'm not sure what your intended purpose of the UIGraphicsBeginImageContext and UIGraphicsEndImageContext calls are, but I can't see that they're needed here.


As for shouldAutoplay = NO, the video is still starting because you are calling play immediately afterwards.


The player's duration property only contains a useful value after a MPMovieDurationAvailableNotification has been received. You'll need to do something similar to the following to have access to the actual duration:

__weak MediaPlayerController *weakSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:MPMovieDurationAvailableNotification object:self.player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
  NSLog(@"Movie duration: %lf", weakSelf.player.duration);
}];

Use removeObserver:name:object: to remove the observer when you are done.

Upvotes: 2

Poonam
Poonam

Reputation: 214

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

Upvotes: 4

Related Questions