Swift 3 sound play

前端 未结 4 1038
我在风中等你
我在风中等你 2021-02-01 22:33

Ok I have looked into this and have tried many different ways to play a sound when a button is clicked.

How would I play a sound when a button is clicked in swift 3? I h

4条回答
  •  一整个雨季
    2021-02-01 23:04

    User below this function

     //MARK:- PLAY SOUND
    func playSound() {
        let url = Bundle.main.url(forResource: "ClickSound", withExtension: "mp3")!
    
        do {
            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }
    
            player.prepareToPlay()
            player.play()
        } catch let error as NSError {
            print(error.description)
        }
    }
    

    first import AudioToolbox import AVFoundation

    Hope it works :)

提交回复
热议问题