player.duration在MPMoviePlayerController中为视频文件显示始终为零 [英] player.duration shows always zero in MPMoviePlayerController for video file
问题描述
我正在使用此代码在mpmediaplayer控制器中播放一个小视频
I am playing a small video in mpmediaplayer controller using this code
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:videostr]];
其中videostr是该视频文件的路径。
where videostr is path of that video file.
现在我需要获取该视频文件的长度,因为我正在使用此代码。
Now i need to get length of that video file for that i am using this code.
length = player.duration;
但它总是显示0.000。但视频播放效果很好。
But it always shows 0.000. But the video is playing well.
我正在通过谷歌搜索每个代码来获取视频时长仅 player.duration
。
I am checking by googling every where code to get video duration is player.duration
only.
我还尝试了其他一些代码
And i try some other code also
AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videostr]
options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], AVURLAssetPreferPreciseDurationAndTimingKey, nil]] autorelease];
NSTimeInterval duration;
if (asset)
duration = CMTimeGetSeconds(asset.duration) ;
NSLog(@">>>>>>>>>>>>>>>>>>>> %f", asset.duration);
即使它显示为零。任何人都可以帮助我。
even though it shows zero.Can any one please help me.
提前感谢。
推荐答案
在内容实际可播放之前,您无法获得有用的持续时间。
You can not get a useful duration until the content is actually playable.
注册加载状态更改:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(MPMoviePlayerLoadStateDidChange:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
收到通知后评估州:
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
if ((player.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
{
NSLog(@"content play length is %g seconds", player.duration);
}
}
这篇关于player.duration在MPMoviePlayerController中为视频文件显示始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!