UIProgressBar for AvAudioPlayer playing progress

2020-07-30 02:46发布

问题:

In my app playing audiofile for that i want to show UIProgressBar on the UIToolbar for audiofile playing progress. Anyone can help me how to code for it.

回答1:

I use a timer to check the current duration, and then update my progress bar every half second, like so:

tmrCounter = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(updateElapsedTime) userInfo:nil repeats:YES];  

-(void)updateElapsedTime{
    if (player) {
        [prgIndicator setProgress:[player currentTime] / [player duration]];
        [lblTime setText:[NSString stringWithFormat:@"%@", [self formatTime:[player currentTime]]]];
    }
}

-(NSString*)formatTime:(float)time{
    int minutes = time / 60;
    int seconds = (int)time % 60;
    return [NSString stringWithFormat:@"%@%d:%@%d", minutes / 10 ? [NSString stringWithFormat:@"%d", minutes / 10] : @"", minutes % 10, [NSString stringWithFormat:@"%d", seconds / 10], seconds % 10];
}

Hope this helps!



标签: iphone