5

我已经实现了 AVCaptureSession 的概念来录制视频。

-(void)startRecordingWithOrientation:(AVCaptureVideoOrientation)videoOrientation 
{

    AVCaptureConnection *videoConnection = [AVCamUtilities   
                                           connectionWithMediaType:AVMediaTypeVideo  
                                           fromConnections:[[self movieFileOutput] connections]];
    if ([videoConnection isVideoOrientationSupported])
        [videoConnection setVideoOrientation:videoOrientation];

    [[self movieFileOutput] startRecordingToOutputFileURL:[self outputFileURL]  
    recordingDelegate:self];
 } 

它正在正确录制视频,但屏幕上没有录制计时器。任何人都知道如何在制作视频时显示计时器。

提前致谢。

4

3 回答 3

8

我使用在录制时将 UILabel 添加到显示视频的视图中,并使用此代码显示录制时间

@property (weak, nonatomic) IBOutlet UILabel *labelTime;

@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic) int timeSec;
@property(nonatomic) int timeMin;

//开始录制的方法

- (void)startRecord {
    self.timeMin = 0;
    self.timeSec = 0;

    //String format 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", self.timeMin, self.timeSec];
    //Display on your label
    //[timeLabel setStringValue:timeNow];
    self.labelTime.text= timeNow;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];

    //Start recording
    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];

}


//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer {
    self.timeSec++;
    if (self.timeSec == 60)
    {
        self.timeSec = 0;
        self.timeMin++;
    }
    //String format 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", self.timeMin, self.timeSec];
    //Display on your label
    self.labelTime.text= timeNow;
}
于 2014-03-06T10:43:12.877 回答
7

@Fran Martin 接受的答案效果很好!

由于我在Swift中使用它,我花了大约一个小时来找出正确的Timer()功能。为了帮助下一个不流利使用Objective C的人,这里是已接受答案的Swift版本,其中包含一些额外的计时器功能,invalidate将计时器重置回00:00,何时使用它viewWillAppear,以及何时使用invalidate

始终 invalidate使用计时器,viewWillDisappear否则viewDidDisappear如果它是repeat计时器并且正在运行,您可以获得memory leak

我遇到了一个无法预料的问题,即使我会停止计时器,它也会继续运行,我发现这个SO Answer说你必须在再次启动它之前停止它以及当你声明计时器使用时weak

@IBOutlet weak fileprivate var yourLabel: UILabel!

var timeMin = 0
var timeSec = 0
weak var timer: Timer?

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // if your presenting this vc yourLabel.txt will show 00:00
    yourLabel.txt = String(format: "%02d:%02d", timeMin, timeSec)
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)

    resetTimerToZero()
}

// MARK:- recordButton
@IBAction fileprivate func recordButtonTapped(_ sender: UIButton) {

    startTimer()
    
    movieFileOutput.startRecording(to: videoUrl, recordingDelegate: self)
}

// MARK:- Timer Functions
fileprivate func startTimer(){
    
    // if you want the timer to reset to 0 every time the user presses record you can uncomment out either of these 2 lines

    // timeSec = 0
    // timeMin = 0

    // If you don't use the 2 lines above then the timer will continue from whatever time it was stopped at
    let timeNow = String(format: "%02d:%02d", timeMin, timeSec)
    yourLabel.txt = timeNow

    stopTimer() // stop it at it's current time before starting it again
    timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
                self?.timerTick()
            }
}
    
@objc fileprivate func timerTick(){
     timeSec += 1
        
     if timeSec == 60{
         timeSec = 0
         timeMin += 1
     }
        
     let timeNow = String(format: "%02d:%02d", timeMin, timeSec)
        
     yourLabel.txt = timeNow
}

// resets both vars back to 0 and when the timer starts again it will start at 0
@objc fileprivate func resetTimerToZero(){
     timeSec = 0
     timeMin = 0
     stopTimer()
}

// if you need to reset the timer to 0 and yourLabel.txt back to 00:00
@objc fileprivate resetTimerAndLabel(){

     resetTimerToZero()
     yourLabel.txt = String(format: "%02d:%02d", timeMin, timeSec)
}

// stops the timer at it's current time
@objc fileprivate stopTimer(){

     timer?.invalidate()
}
于 2018-03-16T14:44:36.420 回答
0

记住开始录制时的时间(NSTimeInterval),将其保存在实例变量中,然后在每秒触发两次左右的计时器中计算与当前时间的差异(NSDate timeIntervalSinceReferenceDate),并在 UITextView 中显示结果时间?

为避免漂移,请在每次显示后在计时器上设置“触发时间”,并将其设置为直到下一整秒(或半秒或无论如何频繁)消失的时间。这样,如果例如显示时间需要 0.1 秒,则下一个触发时间更有可能是整整一秒左右。

于 2014-03-06T10:40:51.010 回答