[英]How to show duration of video record in a textfield
我正在使用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);
}
}
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框架
開始錄制時啟動計時器...
- (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];
}
希望它能對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.