Return to Snippet

Revision: 80649
at March 23, 2020 17:56 by chrisaiv


Updated URL
https://www.chrisjmendez.com/2016/10/23/swift-load-a-wav-sound-using-avfoundation/

Updated Code
https://www.chrisjmendez.com/2016/10/23/swift-load-a-wav-sound-using-avfoundation/

Updated Description
https://www.chrisjmendez.com/2016/10/23/swift-load-a-wav-sound-using-avfoundation/

Revision: 70957
at October 23, 2016 16:59 by chrisaiv


Initial Code
func changeSound(_ song:(path: String, format: String)){
        var err:NSError?
        let track = Bundle.main.path(forResource: song.path, ofType: song.format)!
        let u = URL(fileURLWithPath: track)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: u)
        } catch let error as NSError {
            err = error
            audioPlayer = nil
        }
        if let error = err {
            print("audioPlayer Err: \(error.localizedDescription)")
        } else {
            audioPlayer?.prepareToPlay()
        }
        if let player = audioPlayer {
            player.play()
        }

    }

Initial URL

                                

Initial Description
I sometimes need to dynamically load a .wav file.  Here's how I do it using AVFoundation

Initial Title
Swift: Load a Wav sound using AVFoundation

Initial Tags
iphone

Initial Language
iPhone