How to get an MP3 bit rate in SWIFT

2019-07-23 05:16发布

问题:

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")
        }

回答1:

Just using the formula. It was that easy..

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


回答2:

I found a variable name sampleRate of AVAudioFormat class. Did you try to call audioPlayer.format.sampleRate?