Reputation: 7847
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
Upvotes: 11
Views: 13952
Reputation: 2274
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
}
Upvotes: 11
Reputation: 208
Add a periodic time observer and update the slider - in Swift it will be like this -
func addPlayBackSlider()
{
playbackSlider = UISlider(frame:CGRect(x:10, y:300, width:300, height:20))
playbackSlider?.minimumValue = 0
let duration : CMTime = playerItem.asset.duration
let seconds : Float64 = CMTimeGetSeconds(duration)
playbackSlider?.maximumValue = Float(seconds)
playbackSlider?.isContinuous = true
//If you want to manually set progressBar
playbackSlider?.addTarget(self, action: #selector(AddAudioController.playbackSliderValueChanged(_:)), for: .valueChanged)
//Here is the Observer
player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
if self.player!.currentItem?.status == .readyToPlay {
let time : Float64 = CMTimeGetSeconds(self.player!.currentTime());
self.playbackSlider!.value = Float ( time );
}
}
}
//If you want to manually set progressBar
func playbackSliderValueChanged(_ playbackSlider:UISlider)
{
let seconds : Int64 = Int64(playbackSlider.value)
targetTime = CMTimeMake(seconds, 1)
print(targetTime)
player!.seek(to: targetTime)
if player!.rate == 0
{
player?.play()
playButton!.setTitle("Pause", for: UIControlState.normal)
}
}
Upvotes: 0
Reputation: 7847
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);
}
Upvotes: 19