@@ -20,35 +20,33 @@ public class OnReadAudio {
20
20
var listOfSongs : [ [ String : Any ? ] ] = Array ( )
21
21
22
22
for uri in data {
23
- // ipod-library://item/item.mp3?id=8746509304657824874
24
-
25
23
let url = uri. starts ( with: " ipod-library:// " ) ? URL ( string: uri) ! : URL ( fileURLWithPath: uri)
26
24
27
- // let id3Tag = try! ID3TagEditor().read(from: uri)
28
- // let tagContentReader = ID3TagContentReader(id3Tag: id3Tag!)
29
- // print(tagContentReader.title())
30
-
31
25
let file = try ! AVAudioFile ( forReading: url)
32
26
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
39
28
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 (
41
40
channels: Int64 ( fileFormat. channelCount) ,
42
41
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
45
44
)
46
-
47
45
listOfSongs. append ( info)
48
46
}
49
47
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).
52
50
DispatchQueue . main. async {
53
51
self . result ( listOfSongs)
54
52
}
0 commit comments