Crazy Developer
Crazy Developer

Reputation: 3464

video don't play with MPMoviePlayerController

I want to get total duration of the video in iPhone. I am using the following code tho get the duration

videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:videoPath];
MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 
mpPlayer.view.frame = CGRectMake(0, 0, 320, 100);
[self.view addSubview:mpPlayer.view];
NSLog(@"duration = %f",mpPlayer.duration);
[mpPlayer play];

but I don't know why i am not able to get duration of the video also the video is not get play. can anyone help me to find the solution of this problem.

Thanks in advance.

Upvotes: 1

Views: 1366

Answers (3)

NANNAV
NANNAV

Reputation: 4901

    videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:videoPath];
    MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease]; 
    mpPlayer.view.frame = CGRectMake(0, 0, 320, 100);
    [self.view addSubview:mpPlayer.view];
    NSLog(@"duration = %f",mpPlayer.duration);
    [mpPlayer play];

//add notifications


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
duration = mpPlayer.duration;
NSLog(@"duration = %f",mpPlayer.duration);
}
}       

Upvotes: 0

Till
Till

Reputation: 27587

Hints:

The duration may not be available prior to the actual playback -> check the duration again when receiving a MPMoviePlayerPlaybackStateDidChangeNotification (MPMoviePlayerController.playbackState == MPMoviePlaybackStatePlaying) to be on the safe side.

Upvotes: 2

visakh7
visakh7

Reputation: 26390

Pls check if the path for the video is correct and check if the player is allocated with the correct file url

Upvotes: 0

Related Questions