Skip to content
This repository was archived by the owner on Sep 19, 2023. It is now read-only.

Commit 59fa375

Browse files
author
Gabriele Marcato
committed
bit rate
1 parent dbe26fa commit 59fa375

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

ios/Classes/methods/OnReadAudio.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,33 @@ public class OnReadAudio {
2020
var listOfSongs: [[String: Any?]] = Array()
2121

2222
for uri in data {
23-
// ipod-library://item/item.mp3?id=8746509304657824874
24-
2523
let url = uri.starts(with: "ipod-library://") ? URL(string: uri)! : URL(fileURLWithPath: uri)
2624

27-
// let id3Tag = try! ID3TagEditor().read(from: uri)
28-
// let tagContentReader = ID3TagContentReader(id3Tag: id3Tag!)
29-
// print(tagContentReader.title())
30-
3125
let file = try! AVAudioFile(forReading: url)
3226
let fileFormat = file.fileFormat
33-
let fileDescription = fileFormat.formatDescription
34-
// let streamDescription = fileFormat.streamDescription.pointee;
35-
// let bitDepth = streamDescription.mBitsPerChannel;
36-
// let bitRate = streamDescription.mBytesPerFrame;
37-
let settings = file.fileFormat.settings
38-
let bitDepth = settings.first(where: { $0.key == AVEncoderBitDepthHintKey })?.value as? String ?? nil
27+
let settings = fileFormat.settings
3928

40-
var info = self.toJson(
29+
// bithDepth of the song, available only with lossless formats:
30+
// 'flac', 'wma', 'alac'
31+
let bitDepth = settings.first(where: { $0.key == AVEncoderBitDepthHintKey })?.value as? String
32+
let finalBitDepth: Int64? = bitDepth == nil ? nil : Int64(bitDepth!)
33+
34+
// try to read the song real bitRate
35+
let bitRate = settings.first(where: { $0.key == AVEncoderBitRateKey })?.value as? String
36+
// if cannot read it, calculate it approximately by: length / sampleRate
37+
let finalBitRate: Int64? = bitRate == nil ? Int64(file.length) / Int64(fileFormat.sampleRate) : Int64(bitRate!)
38+
39+
let info = self.toJson(
4140
channels: Int64(fileFormat.channelCount),
4241
sampleRate: Int64(fileFormat.sampleRate),
43-
bitRate: Int64(file.length) / Int64(fileFormat.sampleRate),
44-
bitDepth: bitDepth == nil ? nil : Int64(bitDepth!)
42+
bitRate: finalBitRate,
43+
bitDepth: finalBitDepth
4544
)
46-
4745
listOfSongs.append(info)
4846
}
4947

50-
// After finish the "query", go back to the "main" thread(You can only call flutter
51-
// inside the main thread).
48+
// After finish the "query", go back to the "main" thread
49+
// (You can only call flutter inside the main thread).
5250
DispatchQueue.main.async {
5351
self.result(listOfSongs)
5452
}

lib/details/models/audio_model.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ class AudioModel {
244244
/// Return song [bitrate]
245245
int? get bitrate => _info["BITRATE"];
246246

247+
/// Return song [bitDepth]
248+
int? get bitDepth => _info["BIT_DEPTH"];
249+
247250
/// Return song [channels]
248251
String? get channels => _info["CHANNELS"];
249252

0 commit comments

Comments
 (0)