NSSound.beep() isn't working in macOs (Swift)

454 Views Asked by At

I'm trying to do a an status item with a single button that sounds a beep when pressed.

But nothing is playing when pressing. This is the code:

import SwiftUI
import AppKit


struct MenuView: View {
    @State var currentTab = "Enabled"
    var body: some View {
        VStack{
            HStack{
                
                Button(action: {
                    NSSound.beep()
                    print("Beep")
                }) {
                    Text("Hello wordl!")
                }            }
            .padding(.horizontal)
            .padding(.top)
            
            Spacer(minLength: 0)
            
        }.frame(width: 250, height: 300)
    }
}

struct MenuView_Previews: PreviewProvider {
    static var previews: some View {
        MenuView()
    }
}

Thanks in advance

2

There are 2 best solutions below

0
On BEST ANSWER

Honestly, I can't see the issue with this, it looks fine, it's very simple code so its not like something under the hood could be breaking it and as @Sweeper said it works for him as it does for me.

I know this is gonna sound silly, but more often than I'd like to admit, its the silly things that stump me, are you sure you have you're sound on/turned up?

1
On

As @Ayrton said the problem was in the audio settings. More precisely the problem was the output of the alert sounds, I didn't knew that the alerts could have different output, so once I changed the output to the build-in the code works perfectly fine.