How to play audio from an array using button action?

  1. 2 years ago

    How to play audio from an array using button action?

  2. niutech

    22 Jan 2016 Administrator
    Edited 2 years ago by niutech

    You can use this Swift code:

    func playSound(soundName: String) {
        let soundURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundName, ofType: "mp3")!)
        let audioPlayer = AVAudioPlayer(contentsOfURL:soundURL)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
    
    @IBAction func buttonClicked(sender: AnyObject) {
        let soundArray = ["One"]
        playSound(soundArray[0])
    }

or Sign Up to reply!