【问题标题】:How to set timer between two button clicks如何在两次按钮点击之间设置计时器
【发布时间】:2015-10-26 16:52:32
【问题描述】:

我在屏幕上有一个按钮来启动相机和一个按钮来停止相机。基本上是用来录视频的。所以我只是想设置一个计时器来确定这些视频文件的总时长。

//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);
}
}

【问题讨论】:

  • 我需要获取视频的总时长。我正在使用 AVAsset。但保持零秒:/
  • 改进的语言和标点符号

标签: ios objective-c avfoundation media-player alassetslibrary


【解决方案1】:

您需要创建一个自定义计数器方法。你可以试试这个代码。

创建计时器和计数器属性

@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 获取时间。重新设置时间,然后再做你的事情。

【讨论】:

  • 谢谢,但我应该至少有两位小数来显示时间。@Mr.T
  • 你的意思是你需要 00,01,02,03 秒格式的时间吗?
  • 我想一起看秒和分。喜欢 00.05.. @Mr.T
  • 非常感谢。 @Mr.T
猜你喜欢
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 2014-11-09
  • 1970-01-01
  • 2017-11-29
  • 2022-07-08
  • 1970-01-01
相关资源
最近更新 更多