开发者

Keeping track of time while recording

开发者 https://www.devze.com 2023-02-13 07:04 出处:网络
I have an AVAudioRecorder that records sound. I also have a label. I would like to update the text on the lab开发者_JAVA百科el every second to show the recording time. How can I do this?You can use th

I have an AVAudioRecorder that records sound. I also have a label. I would like to update the text on the lab开发者_JAVA百科el every second to show the recording time. How can I do this?


You can use the currentTime property of AVAudioRecorder (audioRecorder.currentTime) to get the time, as an NSTimeInterval, since the beginning of the recording which u can use to display on your label.


do like this:

  - (IBAction)startStopRecording:(id)sender 
    {
           //If the app is note recording, we want to start recording, and make the record button say "STOP"
        if(!self.isRecording)
        {
            self.isRecording = YES;  //this is the bool value to store that recorder recording
            [self.recordButton setTitle:@"STOP" forState:UIControlStateNormal];

            recorder = [[AVAudioRecorder alloc] initWithURL:recordedFile settings:nil error:nil];
            [recorder prepareToRecord];
            [recorder record];

            myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];  //this is nstimer to initiate update method
        }

        else
        {
            self.isRecording = NO;
            [self.recordButton setTitle:@"REC" forState:UIControlStateNormal];

            [recorder stop];
            recorder = nil;    
            [myTimer invalidate];
        }

    }

- (void)updateSlider {
    // Update the slider about the music time
    if([recorder isRecording])
    {

        float minutes = floor(recorder.currentTime/60);
        float seconds = recorder.currentTime - (minutes * 60);

        NSString *time = [[NSString alloc] 
                                    initWithFormat:@"%0.0f.%0.0f",
                                    minutes, seconds];
        recordTimeLabel.text = time;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消