Playing music using AVPlayer

February 17, 2019

A common task in a typical iOS Application is the ability to play a particular sound. In order to do so, you will need to leverage AVPlayer and AVAudioPlayer in order to download a song/sound from the internet and play it within the app.

/// Play a sound from a URL. 
func play(_ url:NSURL) {
    do {
        self.player = try AVAudioPlayer(contentsOfURL: url)
    } catch let error as NSError {
        // An error has occurred
    } catch {
        // Could not init AVAudioPlayer