我正在使用 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);
}
}