0

I have a progress view that represents the playing/progress of a audio track as it is played. 1.0 represents 100% completion of that progress view of course. What I need to accomplish is the progress view's progress gradually increasing throughout the duration of the audio track - need to reach 100% progress (progress=1.0) at the same time the audio track is finished. I do have the duration value of the audio track as well. What I'm confused about is the math or how to get gradually increase progress of progress view based on the duration of the audio track.

Note: I will have an array of audio tracks that have different durations. Also, I am using AVPlayer to play these audio tracks.

Any help would be greatly appreciated. Thanks in advance.

Mike Simz
  • 3,976
  • 4
  • 26
  • 43
  • take a look at this https://github.com/jdg/MBProgressHUD they have a module for progress bar, might be useful for you – Ricardo Alves Jan 13 '16 at 17:45
  • I've checked MBProgressHUD out. I'm more-so looking for the math to achieve what I am looking for @RicardoAlves – Mike Simz Jan 13 '16 at 17:46

2 Answers2

0

After doing some more digging, I believe approaching it this way will work:

AVPlayerItem *currentItem = yourAVPlayer.currentItem;

CMTime duration = currentItem.duration; //total time
CMTime currentTime = currentItem.currentTime; //playing time

And then in the update method...

 progressView.progress = (CGFloat)currentTime / (CGFloat)duration;

Can someone please confirm if this will work as I am not at my workstation at the moment.

Mike Simz
  • 3,976
  • 4
  • 26
  • 43
0

A simpler approach is to allow the maximum and minimum values of the progress view to be set. For example, in my app, I use a UISlider to show progress. When the user selects a track to play, I set the minimum and maximum values of the slider as follows:

_slider.minimumValue = 0;
_slider.maximumValue = itemDuration;

The progress value is then just the current time of the player.

[_slider.value setValue:currentPlayerTime animated:YES];

You do not say if your progress view uses a UISlider, but even if it does not, you can probably use this approach.