[英]show record timer while making video
我已經實現了用於錄制視頻的 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];
}
它正在正確錄制視頻,但屏幕上沒有錄制計時器。 任何人都知道如何在制作視頻時顯示計時器。
提前致謝。
我在錄制時向顯示視頻的視圖中添加一個 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;
}
@Fran Martin 接受的答案效果很好!
因為我在Swift中使用它,所以我花了大約一個小時來找出正確的Timer()
函數。 為了幫助下一個不熟練使用 Objective C 的人,這里是已接受答案的Swift版本,其中包含一些額外的功能來invalidate
計時器invalidate
將計時器重置回00:00
,何時在viewWillAppear
使用它,以及何時使用它invalidate
始終invalidate
viewWillDisappear
或viewDidDisappear
的計時器invalidate
,否則如果它是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()
}
記住開始錄制時的時間 (NSTimeInterval),將其保存在實例變量中,然后在每秒觸發兩次左右的計時器中計算與當前時間的差異 (NSDate timeIntervalSinceReferenceDate),並在 UITextView 中顯示結果時間?
為避免漂移,請在每次顯示后在計時器上設置“觸發時間”,並將其設置為距離下一整秒(或半秒或多長時間)的時間。 這樣,例如,如果顯示時間需要 0.1 秒,則下一次觸發時間更有可能在整整一秒左右。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.