Reputation: 474
I'm trying to set a progressbar and a label to indicate the % of a mp3 file being played. Do I need to set a NSTimer function or can I do it with the AVFoundation?
Upvotes: 2
Views: 2072
Reputation: 4044
Accomplish what you want using the following information which is available by AVFoundation. Use duration of mp3 file which you can get by creating an AVAsset
with your mp3 file. And use AVAudioPlayer.currentTime
to get the current playback position of the currently playing audio file.Periodically check the value of currentTime
and update the progress bar accordingly. You can use a while-loop
to constantly check currentTime
which will make the progress bar appear to increment smoothly. You could also use an NSTimer but depending on your time intervals the progress bar may not appear to increment smoothly but it may appear abrupt.
Alternatively you could use AVPlayer
or AVQueuePlayer
instead of AVAudioPlayer
. With those you will create an AVPlayerItem
from which you can get the duration of the current song by AVPlayer.currentItem.duration
.
Upvotes: 1