MPMoviePlayerController - 持续时间始终为 0
iPhone4、iOS 4.3.3、iOS SDK4.3
大家好,
我正在创建一个视频上传功能。视频是使用 UIImagePickerController 检索的,并且可以使用相机捕获或从照片库中选取。我的应用程序最大持续时间限制为 60 秒。使用相机录制视频时,可以通过以下方式轻松实现:
// 将视频限制为 60 秒
[picker setVideoMaximumDuration:60];
但是,当从照片库中选择视频时,我看到获取持续时间的唯一方法是通过 MPMoviePlayerController 持续时间属性,如下所示:
// MediaType 可以是 kUTTypeImage 或 kUTTypeMovie。
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType]; NSLog(@"%@",mediaType);
// 如果它是电影
if ( [mediaType isEqualToString:(NSString*)kUTTypeMovie] ) {
// get the URL
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@",mediaURL);
// can use MPMoviePlayerController class to get duration
int duration = -1;
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: mediaURL];
if (moviePlayer != nil) {
duration = moviePlayer.duration;
NSString *path = moviePlayer.contentURL;
[moviePlayer release];
}
,但持续时间始终为 0。我知道视频有持续时间,因为在照片库中选择它时,持续时间会显示为字幕的一部分。我知道持续时间可能并不总是可用,但在这种情况下,持续时间显示在照片库中。我还检查了 contentURL 属性,它有一个很好的值。我能够检索该文件,获取其文件大小等,因此我知道该文件的 NSURL 很好......
谢谢!
iPhone4, iOS 4.3.3, iOS SDK4.3
Hi all,
I'm creating a video upload feature. The videos are retrieved using UIImagePickerController and can be captured using the camera or picked from the photo library. I have an application constraint of 60 seconds max duration. This is easily achieved when recording the video using the camera via:
// Limit videos to 60 seconds
[picker setVideoMaximumDuration:60];
However when the video is selected from the photo library the only way I see of obtaining the duration is via MPMoviePlayerController duration property as follows:
// MediaType can be kUTTypeImage or kUTTypeMovie.
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSLog(@"%@",mediaType);
// if its a movie
if ( [mediaType isEqualToString:(NSString*)kUTTypeMovie] ) {
// get the URL
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@",mediaURL);
// can use MPMoviePlayerController class to get duration
int duration = -1;
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: mediaURL];
if (moviePlayer != nil) {
duration = moviePlayer.duration;
NSString *path = moviePlayer.contentURL;
[moviePlayer release];
}
however the duration is always 0. I know the video has a duration because the duration is displayed as part of the subtitle when selecting it in the photo library. I understand that duration may not always be available but in this case the duration is displayed in photo lib. I also check the contentURL property and it has a good value. I'm able to retrieve the file, get its filesize etc so I know the NSURL of the file is good...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有立即发现您的代码有任何问题。然而,使用 AVFoundation 对于此类操作要快得多。下面是使用 AVFoundation 获取持续时间的代码片段:
I don't see anything immediately wrong with your code. However, using AVFoundation is much faster for this type of operation. Here's a code snippet to get the duration using AVFoundation: