MPMoviePlayerController-持续时间始终为0 [英] MPMoviePlayerController - Duration always 0

查看:70
本文介绍了MPMoviePlayerController-持续时间始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone4,iOS 4.3.3,iOS SDK4.3

iPhone4, iOS 4.3.3, iOS SDK4.3

大家好,

我正在创建视频上传功能.使用UIImagePickerController检索视频,可以使用相机捕获视频,也可以从照片库中选择视频.我的应用程序限制为最大持续时间为60秒.通过以下方式使用摄像机录制视频时,很容易做到这一点:

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:

//将视频限制为60秒

// Limit videos to 60 seconds

[picker setVideoMaximumDuration:60];

[picker setVideoMaximumDuration:60];

但是,当从照片库中选择视频时,我看到的获取持续时间的唯一方法是通过MPMoviePlayerController duration属性,如下所示:

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可以是kUTTypeImage或kUTTypeMovie.

// MediaType can be kUTTypeImage or kUTTypeMovie.

NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType]; NSLog(@%@",mediaType);

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很好...

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...

谢谢!

推荐答案

我没有发现您的代码立即出现任何问题.但是,对于这种类型的操作,使用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:

AVURLAsset *asset = [[[AVURLAsset alloc] initWithURL:anURI 
                      options:[NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithBool:YES], 
                      AVURLAssetPreferPreciseDurationAndTimingKey,
                      nil]] autorelease];

NSTimeInterval durationInSeconds = 0.0;
if (asset) 
    durationInSeconds = CMTimeGetSeconds(asset.duration) ;

这篇关于MPMoviePlayerController-持续时间始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆