Would anybody have a link to a tutorial to add a playback progress bar to AVAudioPlayer?
I've searched extensively on this site and on google to no avail
The CADisplayLink
class, which automatically calls a method you define as soon as a screen redraw happens
Timer
doesn't offer precise firing and can drift earlier or later than requested updates, and also has no idea about screen redraws and so could happily fire 10ms after a screen redraw just happened.
let displayLink = CADisplayLink(target: self,
selector: #selector(update))
displayLink.add(to: .current, forMode: .common)
@objc func update() {
let currentTime = avAudioPlayer.currentTime
let totalTime = avAudioPlayer.duration
let progress = currentTime / totalTime
}
I figured it out and it wasnt too bad.
Just update it in a timer
playbackTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(myMethod:)
userInfo:nil
repeats:YES];
}
-(void)myMethod:(NSTimer*)timer {
float total=audioPlayer.duration;
float f=audioPlayer.currentTime / total;
NSString *str = [NSString stringWithFormat:@"%f", f];
playbackProgress.progress=f;
NSLog(str);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With