Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

avaudioplayer playback progress

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

like image 925
dubbeat Avatar asked Feb 24 '10 10:02

dubbeat


2 Answers

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
}
like image 167
Paul Nyondo Avatar answered Oct 20 '22 00:10

Paul Nyondo


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);
}
like image 26
dubbeat Avatar answered Oct 20 '22 00:10

dubbeat