I have AVAudioPlayer initialized in my macOS app like below:
do {
let audioFile = Bundle.main.path(forResource: "sample", ofType: "mp3")!
let audioURL = URL(filePath: audioFile)
player = try AVAudioPlayer(contentsOf: audioURL)
} catch {
fatalError()
}
When I click the "Start" button it starts to play sound correctly but the printed dBLevel is always -160.
Button("Start") {
player!.play()
var dbLevel = Float(0)
timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true, block: { _ in
player!.updateMeters()
dbLevel = 0
for n in 0..<player!.numberOfChannels {
dbLevel += player!.averagePower(forChannel: n)
}
dbLevel = dbLevel / Float(player!.numberOfChannels)
print(dbLevel)
})
}
Note that Xcode writes the following message to the console
AddInstanceForFactory: No factory registered for id <CFUUID 0x600002d8d0e0>...
What I'm doing wrong?
I found the solution quickly - this line was needed: