我希望在iPhone中获得视频的总持续时间。我使用以下代码获取持续时间
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];
但我不知道为什么我无法获得视频的持续时间,视频也无法播放。 任何人都可以帮我找到解决这个问题的方法。
提前致谢。
答案 0 :(得分:2)
提示:
在实际播放之前,持续时间可能无法使用 - >收到MPMoviePlayerPlaybackStateDidChangeNotification
后,请再次检查持续时间(MPMoviePlayerController.playbackState == MPMoviePlaybackStatePlaying)
是安全的。
答案 1 :(得分:0)
请检查视频路径是否正确,并检查播放器是否分配了正确的文件URL
答案 2 :(得分:0)
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);
}
}