0

我正在使用 AVFoundation 录制视频文件并将其保存到相机胶卷。但同时我想在一个简单的文本字段中显示这些视频的持续时间。有什么想法吗?

//我已经分离为 2 种方法,但仍然需要 0 秒来停止凸轮

- (IBAction)startCamera:(id)sender {

if (!recording)
{
    //----- START RECORDING -----
    NSLog(@"START RECORDING");
    recording = YES;


 //Create temporary URL to record to
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:outputPath])
    {
    }
    //Start recording
    [MovieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];     

AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
    CMTime videoDuration = videoAsset.duration;
    float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

    NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];  //we can vary the date string. Ex: "mm:ss"
    NSString* result = [dateFormatter stringFromDate:date];
    NSLog(@"Duration 1 of this after starting : %2@",result);

//停止相机方法

-(void)stopCamera:(id)sender {

//----- STOP RECORDING -----
    NSLog(@"STOP RECORDING");
    recording = NO;
    [MovieFileOutput stopRecording];

//Create temporary URL to record to
    NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
    NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:outputURL options:nil];
    CMTime videoDuration = videoAsset.duration;
    float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

    NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [dateFormatter setDateFormat:@"HH:mm:ss"];  //we can vary the date string. Ex: "mm:ss"
    NSString* result = [dateFormatter stringFromDate:date];
    NSLog(@"Duration 2 of this after stopping : %2@",result);
}
}
4

2 回答 2

0

开始录制时启动计时器...

- (IBAction)startCamera:(id)sender {

if (!recording)
{
      //----- START RECORDING -----
      NSLog(@"START RECORDING");
      recording = YES;

 _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                              target:self
                                            selector:@selector(getCountInSec:)
                                            userInfo:nil
                                             repeats:YES];


     //Create temporary URL to record to

else {
  //----- STOP RECORDING -----
NSLog(@"STOP RECORDING");
recording = NO;

if ([_timer isValid]) {
    [_timer invalidate];
}
_timer = nil;
[MovieFileOutput stopRecording];

  }
 }

// 这里是定时器方法..

-(void)getCountInSec
 {   
  // set a global variable of int type . every time increment by one & manage as you need (Sec ,min or hours). 

     yourlbl.text = [NSString stringWithFormate@"%ld",yourVarable];

}

希望对你有帮助。

于 2015-10-26T09:41:42.483 回答
0
AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];
CMTime videoDuration = videoAsset.duration;
float videoDurationSeconds = CMTimeGetSeconds(videoDuration);

NSDate* date = [NSDate dateWithTimeIntervalSince1970:videoDurationSeconds];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"HH:mm:ss"];  //you can vary the date string. Ex: "mm:ss"
NSString* result = [dateFormatter stringFromDate:date];

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

于 2015-10-26T09:36:21.110 回答