Ray King Ray King - 1 month ago 10
Swift Question

How to get an MP3 bit rate in SWIFT

I am searching for a way of getting an mp3 bitrate like

128kbps
or
320kbps
for mp3 audio from url link.

I have a
UITableView
that loads a list of files from url list, and I would like to display an audio quality.
I have tried using
AVAudioPlayer
and
AVPlayer
but no luck.
Please help, how can I achieve this?

do{
let audioPlayer = try AVAudioPlayer(contentsOf:audioURL)
print(audioPlayer.settings)
if #available(iOS 10.0, *) {
print(audioPlayer.format)
} else {
// Fallback on earlier versions
}
}catch {
print("Error getting the audio file")
}

Answer

Just using the formula. It was that easy..

var bitrate: Int {      // kbps
        if size > 0 && duration > 0 {
            return size * 8 / 1000 / duration
        }
Comments