I'm trying to update my SwiftUI view when system volume changes. Ultimately, my use case is to display a low volume warning overlay when the system volume is low. Right now, though, let's say I'm just trying to show the current volume in a Text label. I've tried using onReceive with a publisher on AVAudioSession.sharedInstance().outputVolume, as in below, but my onReceive block only fires once when the view appears, and is not called when the volume subsequently changes. Why does the below not work, and what is the best way to update SwiftUI views when volume changes?
struct Test: View {
@State var volume: Float = 0
var body: some View {
Text("current volume is \(volume)")
.onReceive(AVAudioSession.sharedInstance().publisher(for: \.outputVolume), perform: { value in
self.volume = value
})
}
}
Thanks!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Is there a way to apply color filters to the contents of a PDF displayed in a PDFView? My desired effect is to display the PDF with colors inverted. CIFilter and compositingFilter seem relevant, but I couldn't get to a working example. Any guidance appreciated!