【问题标题】:how to get the time duration of recorded video in iphone app如何在iphone应用程序中获取录制视频的持续时间
【发布时间】:2013-06-19 06:03:56
【问题描述】:

我正在 iPhone 中录制视频我想获取录制的视频持续时间。有什么方法可以获取录制的视频时长。

这是我获取录制视频和路径的代码。

if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
{
    NSURL*videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"dd-MM-yyyy_HH:mm:SS"];

    NSDate *now = [[[NSDate alloc] init] autorelease];
    NSDate* theDate = [dateFormat stringFromDate:now];

    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

    NSString*testUser=TitleTextField.text;

    NSString *videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,testUser]] autorelease];

    BOOL success = [videoData writeToFile:videopath atomically:NO];

    // Alert for showing the record video

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"You have recorded a video"
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    recordedVideoLabel.text=@"You have recorded Video";
}

【问题讨论】:

    标签: iphone objective-c video-recording


    【解决方案1】:

    使用 AVAssets 可以轻松完成

    AVURLAsset *avUrl = [AVURLAsset assetWithURL:@"Here comes the url of the video"];
    CMTime time = [avUrl duration];
    int seconds = ceil(time.value/time.timescale);
    

    您需要在项目中链接 CoreMedia 和 AVFoundation 框架

    【讨论】:

      【解决方案2】:

      这对我来说可以以秒为单位获得时间,您可以将此方法添加到您的代码中:

      -(float) getDurationInSecondsForVideoPath:(NSString*)videoPath {
         NSURL * videoURL = [NSURL URLWithString: videoPath];
         AVURLAsset * avAsset = [AVURLAsset videoURL];
         CMTime avDuration = [avAsset duration];
         float avDurationInSeconds = CMTimeGetSeconds(avDuration);
         return avDurationInSeconds;
      }
      

      注意:在使用方法前确保videoPath有效,否则方法会返回0.0000或nan

      欲了解更多信息:AVAsynchronousKeyValueLoadingAVURLAssetAVAsset

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多