作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在屏幕上有一个用于启动相机的按钮和一个用于停止相机的按钮。基本上是用来录视频的。所以我只是想设置一个计时器来确定这些视频文件的总持续时间。
//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 得到时间。重新设置时间,然后再做你的事情。
关于ios - 如何在两次按钮点击之间设置计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33351249/
我是一名优秀的程序员,十分优秀!