记录时记录时间

use*_*375 4 cocoa-touch avfoundation avaudiorecorder ios

我有一个录制声音的AVAudioRecorder.我也有一个标签.我想每秒更新标签上的文字以显示录制时间.我怎样才能做到这一点?

vis*_*kh7 7

您可以使用AVAudioRecorder(audioRecorder.currentTime)的currentTime属性来获取NSTimeInterval自录制开始以来可用于在标签上显示的时间.


Dil*_*lip 6

这样做:

  - (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;
    }
}
Run Code Online (Sandbox Code Playgroud)