How to use an AVAudioPlayer in iOS with Swift 5

import AVFoundation

var player: AVAudioPlayer?


let url = Bundle.main.path(forResource: "richtig", ofType: "wav")
do {
    try AVAudioSession.sharedInstance().setMode(.default)
    try AVAudioSession.sharedInstance().setActive(true, options: .notifyOthersOnDeactivation)
    guard let url = url else {
        return
    }
    player = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: url))
} catch {
    print("Something went wrong")
}

@IBAction func playButton(_ sender: Any) {
    guard let player = player else {
    return
    }
    if player.isPlaying {
        player.stop()
    }
    else {
        player.play()
    }
}