Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timer between two button clicks

I have one button on the screen to start a camera and one button to stop the camera. Basically it's for video recording. So I am just trying to set a timer to determine the total duration of these video files.

//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);
}
}
like image 734
dicle Avatar asked Jun 10 '26 23:06

dicle


1 Answers

You need to make a custom counter method. YOu can try this code.

Create timer and counter properties

@property (nonatomic,strong) NSTimer *startTimer;
@property (assign) NSInteger x;

and give the x 0 in viewDidAppear.

and start the timer when you start recording:

 -(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];

}

and you can get the time from _x. Reset the time and again do your stuff.

like image 190
Teja Nandamuri Avatar answered Jun 13 '26 17:06

Teja Nandamuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!