问题描述
|
我想获取iPhone中视频的总时长。我正在使用以下代码来获取持续时间
videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:videoPath];
MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
mpPlayer.view.frame = CGRectMake(0,320,100);
[self.view addSubview:mpPlayer.view];
NSLog(@\"duration = %f\",mpPlayer.duration);
[mpPlayer play];
但我不知道为什么我无法获取视频的时长,也无法播放视频。
谁能帮助我找到解决此问题的方法。
提前致谢。解决方法
提示:
持续时间可能在实际播放之前不可用->收到
MPMoviePlayerPlaybackStateDidChangeNotification
(出于安全考虑,the2ѭ)时,请再次检查持续时间。,请检查视频路径是否正确,并检查播放器是否分配了正确的文件网址, videoPath = [videoPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:videoPath];
MPMoviePlayerController *mpPlayer = [[[MPMoviePlayerController alloc] initWithContentURL:url] autorelease];
mpPlayer.view.frame = CGRectMake(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);
}
}