In my iOS Swift application, and i am trying to play sound on click of a button.
func playSound()
{
var audioPlayer = AVAudioPlayer()
let
You just need to move the declaration of your audioPlayer out of your method. Try like this:
Swift 3 or later
var audioPlayer = AVAudioPlayer()
func playSound() throws {
let url = Bundle.main.url(forResource: "doorbell", withExtension: "mp3")!
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
do {
try playSound()
} catch {
print(error)
}