如何在两次按钮点击之间设置计时器
How to set timer between two button clicks
我在屏幕上有一个按钮可以启动相机,还有一个按钮可以停止相机。基本上是用来录视频的。所以我只是想设置一个计时器来确定这些视频文件的总持续时间。
//start camera
- (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];
//stop camera
-(void)stopCamera:(id)sender {
//----- STOP RECORDING -----
NSLog(@"STOP RECORDING");
recording = NO;
[MovieFileOutput stopRecording];
//i have used this for get the time but keep saying 00.00.00
//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);
}
}
您需要制作自定义计数器方法。你可以试试这个代码。
创建计时器和计数器属性
@property (nonatomic,strong) NSTimer *startTimer;
@property (assign) NSInteger x;
并给出 viewDidAppear
中的 x 0。
并在开始录制时启动计时器:
-(IBAction)start:(id)sender{
[self startReading];
_startTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(count)
userInfo:nil
repeats:YES];
}
-(IBAction)stop:(id)sender{
[self stopReading];
[_startTimer invalidate];
}
-(void)count{
NSLog(@"video duration %ld",(long)_x);
_x=_x+1;
double seconds = fmod(_x, 60.0);
double minutes = fmod(trunc(_x / 60.0), 60.0);
double hours = trunc(_x / 3600.0);
self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds];
}
您可以从 _x 获取时间。重新设置时间,然后再做你的事情。
我在屏幕上有一个按钮可以启动相机,还有一个按钮可以停止相机。基本上是用来录视频的。所以我只是想设置一个计时器来确定这些视频文件的总持续时间。
//start camera
- (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];
//stop camera
-(void)stopCamera:(id)sender {
//----- STOP RECORDING -----
NSLog(@"STOP RECORDING");
recording = NO;
[MovieFileOutput stopRecording];
//i have used this for get the time but keep saying 00.00.00
//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);
}
}
您需要制作自定义计数器方法。你可以试试这个代码。
创建计时器和计数器属性
@property (nonatomic,strong) NSTimer *startTimer;
@property (assign) NSInteger x;
并给出 viewDidAppear
中的 x 0。
并在开始录制时启动计时器:
-(IBAction)start:(id)sender{
[self startReading];
_startTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(count)
userInfo:nil
repeats:YES];
}
-(IBAction)stop:(id)sender{
[self stopReading];
[_startTimer invalidate];
}
-(void)count{
NSLog(@"video duration %ld",(long)_x);
_x=_x+1;
double seconds = fmod(_x, 60.0);
double minutes = fmod(trunc(_x / 60.0), 60.0);
double hours = trunc(_x / 3600.0);
self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds];
}
您可以从 _x 获取时间。重新设置时间,然后再做你的事情。