Play sound in SWIFT 5

yipkayan

yipcodes

Posted on June 15, 2022

Play sound in SWIFT 5
import AVKit

class ViewController: UIViewController {
    var player: AVAudioPlayer?

    @IBAction func buttonPressed(_ sender: UIButton) {
        playSound(soundTitle:sender.currentTitle!)
    }

    func playSound(soundTitle:String) {
        let path = Bundle.main.path(forResource: "soundName",ofType:"mp3")

        let url = URL(fileURLWithPath: path!)

        do {
            player = try AVAudioPlayer(contentsOf: url)
            player?.play()

        } catch let error {
            print(error.localizedDescription)
        }
    }
}


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
yipkayan
yipcodes

Posted on June 15, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related