Reputation: 3464
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
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
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
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