0

Already finished implementing the player. I want to implement the progress bar. But I wonder if that's possible to do since we are streaming the music. Unless we are provided the length of the song before hand.

Please, I need your advice on this.

RoundOutTooSoon
  • 9,821
  • 8
  • 35
  • 52

2 Answers2

3

Unless you are given the size (or time length) of the audio file beforehand, there's no way you can set the range of your progress bar (you'll have no max).

fbrereto
  • 35,429
  • 19
  • 126
  • 178
1

Actually, I found a way to do that and I succeeded. That you calculate the number of frames per packet of the song, and the average packet's size (in bytes). Use NSHTTPConnection to get the file size and use this formular:

totalFrames = (fileSize * framesPerPacket / average packet size)

when you have total frames, just divide it by the bitrate, then u get urself the total time!!

RoundOutTooSoon
  • 9,821
  • 8
  • 35
  • 52
  • This is slightly inaccurate because it doesn't account for space used up by metadata, file headers, etc. The discrepancy should be small, but ideally there'd be a way of eliminating it entirely. Unfortunately the API that should provide useful results (`AudioFileStreamGetProperty (stream, kAudioFileStreamProperty_AudioDataPacketCount, ...)`) never seems to actually provide a useful result, so unless there's another way I haven't found yet, this looks like the best option available. – Jules Jul 19 '18 at 19:51
  • Of course, there's always `kAudioFileStreamProperty_AudioDataByteCount`, which if it is present should be accurate... – Jules Jul 19 '18 at 22:00