Toggle button not working

I have a toggle button linked to a class for my sound effects. The issue is when i set the button to false the sound effects will still play.

// boolean
@State private var seOn: Bool = true
// var declaration
@ObservedObject var soundEffectsM = soundEffectsModel()
// toggle button
Toggle(isOn: self.$soundEffectsM.seOn)
// example of sound effect
.onTapGesture(perform: {self.soundEffectsM.playSound(sound: "GunCock", type: "wav")})
// sound effect class
class soundEffectsModel: ObservableObject {
    var audioPlayer:AVAudioPlayer?
    @Published var seOn: Bool = UserDefaults.standard.bool(forKey: "seOn") {
        didSet {
            UserDefaults.standard.set(self.seOn, forKey: "seOn")
        }
    }
    func playSound(sound: String, type: String) {
        if let path = Bundle.main.path(forResource: sound, ofType: type) {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                audioPlayer?.play()
            } catch {
                print("ERROR: Could not find sound file!")
            }
        }
    }
}

you need to somehow put an audioPlayer?.stop() when seOn is false

this is to ensure that the player (because it is global) is not playing anymore then you toggle it (even if it wasn’t even turned on in the first place

Ok, thank you. I’ve been trying to give that a go unsuccessfully, also i couldn’t find anything on stackoverflow. Do you perhaps have an example of what that might look like?
I was also going to use if statements but when i try and make the bool a ObservedObject i get an error saying: Generic struct ‘ObservedObject’ requires that ‘Bool’ conform to ‘ObservableObject’. Then if i make it ObservableObject i get an error saying: Unknown attribute ‘ObservableObject’.

@fuerte.francis you where right. So this solved it:

func playSound(sound: String, type: String) {
        if let path = Bundle.main.path(forResource: sound, ofType: type) {
            do {
                audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
                audioPlayer?.play()
            } catch {
                print("ERROR: Could not find sound file!")
            }; if self.seOn == false { pauseMusic() }
        }
    }
    func pauseMusic() {
        UserDefaults.standard.set(false, forKey: "seOn"); audioPlayer?.pause()
    }

I added the pauseMusic function and called it using an if statement. I found a good example from a book called swift game development.