UIProgress View in Recording app
I am adding uiprogreesView in recording app.When I tap start recording button It will take few seconds to start recording and my progress view starts ,I also manged uilabels to get the recorded time.But my progess view is not synchronized with recording time.Actual recording time is less then progress view label.. Here is my code
- (IBAction)startRecordingButtonPressed:(id)sender {
recorder = [[AVAudioRecorder alloc]initWithURL:recordedFile
settings:recordSetting error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder recordForDuration:(NSTimeInterval)60];
[recorder record];
timeStartLabel.text != "0";
progressView.progress = 0.0;
[NSThread detachNewThreadSelector:@selector(startjob)
toTarget:self withObject:nil];
}
- (void )startjob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSThread sleepForTimeInterval:2];
[self performSelectorOnMainThread:@selector(m开发者_JAVA百科oreProgress)
withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)moreProgress {
float actual = [progressView progress];
timeStartLabel.text = [NSString stringWithFormat:@"%.2f" ,actual];
if (actual < 1) {
progressView.progress = actual + 0.01;
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(moreProgress)
userInfo:nil repeats:NO];
}
}
please help.And when I tap record button then button will go blue for few seconds and then I backs to its default color, then actual recording started but progress view started when button tapped.
You can use enable NSTimer to the given interval and in addition to use CADisplayLink that will enable you to update your display in accordance with time progress (You can get the fireDate from the NSTimer at any given point and update you progress bar according to that).
When NSTimer fires, you invalidate your display link (or you can just pause it for future replays..).
Example:
self.displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
and then in the drawView method :
NSDate* currTS = [NSDate date];
NSDate* fireTS = [self.captureTimer fireDate];
NSTimeInterval tsInterval = [fireTS timeIntervalSinceDate:currTS];
float newProgress = 1.0f - (tsInterval / self.intervalFromDefault);
when self.captureTimer is the NSTimer you've set with record method.
Assuming the progress is to show the time elapsed in some manner, can't you use recorder.currentTime
in your updates? The timer stays as it is but the label and progress are update based on this property.
progressView.progress = recorder.currentTime / totalDuration;
timeStartLabel.text = [NSString stringWithFormat:@"%.2f", progressView.progress];
This way the progressView
doesn't get updated before the actual recording starts.
EDIT
- (void) moreProgress {
progressView.progress = recorder.currentTime / totalDuration; // Have totalDuration as ivar or replace it with '60' same as passed during recorder initialization.
timeStartLabel.text = [NSString stringWithFormat:@"%.2f", progressView.progress];
if ( recorder.recording ) {
[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(moreProgress)
userInfo:nil repeats:NO];
}
}
Also if paused the timer won't be set. Either you can have a repeating timer and save a reference to it, or restart it in the delegate method audioRecorderEndInterruption:withFlags:
.
精彩评论