3

I would like to add a sound effect when a button is pressed in my app for SwiftUI. The button has another feature, but I just want to add a sound effect. I have found information for previous versions of Xcode using UIkit, but I'm not understanding how to do it in SwiftUI.

Edit: I'm very new to programming if that explains my lack of knowledge.

  • You should be able to add an [AVAudioPlayer](https://developer.apple.com/documentation/avfoundation/avaudioplayer) to the button's action parameter. – Raisen Jul 27 '20 at 23:01

1 Answers1

10

from answer: Play reminder sound SwiftUI

you can use this simple method:

import AVFoundation

AudioServicesPlaySystemSound(1026)

where the number 1026 is the SystemSound id.