Hi there, second topic here.
I'm using SimplyCoreAudio from Github to animate volume.
My Stack looks like:
ZStack{
Text("Some Text Here")
.font(.custom("Impact", size: isMuted ? 50 : 90).width(.compressed))
.fontWeight(.black)
}
and i'm listening for output device changes like this:
.onReceive(NotificationCenter.default.publisher(for: .defaultSystemOutputDeviceChanged)){ _ in
if let outputDevice = simplyCA.defaultOutputDevice {
if outputDevice.name.contains("AirPods") {
isAirPodsConnected = true
} else {
isAirPodsConnected = false
}
}
}
If "isAirPodsConnected" (Airpods not connected) is false, no problem.
The problem is when i connect my Airpods, the isMuted
variable is triggered and stay as true
(still able to hear sound). Only when Airpods are connected!!
This make the text appear small, big when the volume change but goes back to small after no time... and if really muted, the text appear as big... it works the reverse way...
.onReceive(NotificationCenter.default.publisher(for: .deviceMuteDidChange)){ _ in
isMuted.toggle()
}
If i delete isMuted.toggle()
, problem solved, but no animation...
Why the isMuted
variable is "linked" to Airpods? Am i doing something wrong or missing something? Can i have some help please?
Thanks.
Here's my full code:
import SwiftUI
import SimplyCoreAudio
struct SliderView: View {
@State var isAirPodsConnected = VolumeWindow.isAirPodsConnected
@State private var outputVolume: Float = 0.0
@State private var isMuted = false
@State private var timer: Timer?
var body: some View {
ZStack{
Text("Some Text Here")
.font(.custom("Impact", size: isMuted ? 50 : 90).width(.compressed))
.fontWeight(.black)
}
.frame(width: 180, height: 100)
.onReceive(NotificationCenter.default.publisher(for: .defaultSystemOutputDeviceChanged)){ _ in
if let outputDevice = simplyCA.defaultOutputDevice {
if outputDevice.name.contains("AirPods") {
isAirPodsConnected = true
} else {
isAirPodsConnected = false
}
}
}
.onReceive(NotificationCenter.default.publisher(for: .deviceVolumeDidChange)){ _ in
isMuted = false
if let currentVolume = simplyCA.defaultOutputDevice?.virtualMainVolume(scope: .output) {
self.outputVolume = currentVolume
if self.outputVolume == 0 {
isMuted = true
} else {
isMuted = false
}
}
}
.onReceive(NotificationCenter.default.publisher(for: .deviceMuteDidChange)){ _ in
isMuted.toggle()
}
}
}