2

I'm playing an mp3 file stored in an iOS app's asset catalog & I'm declaring my AVAudioPlayer when I declare the player with app-wide scope. My code works fine, but I'd like to know if this is poor practice & if so, why. Most code examples playing audio declare an AVPlayer as an optional but don't create the player at declaration, like so:

var audioPlayer: AVAudioPlayer?

I implement below b/c doesn't have any references to optionals or forced unwrapping (I'm just ramping up in Swift myself, but I'm using this as an early "Get Excited" example for my students so they can have media playing back in one of their first apps. Hope to postpone optionals discussion 'til later).

// Declare AVAudioPlayer and assign an empty player

var audioPlayer = AVAudioPlayer()

// Call this function to play an mp3 sound in asset file named "sound0"

func playSound() {
    if let sound = NSDataAsset(name: "sound0") {
        do {
            try audioPlayer = AVAudioPlayer(data: sound.data)
            audioPlayer.play()
        } catch {
            print("ERROR: Couldn't create the AVAudioPlayer")
        }
    } else {
        print("ERROR: Couldn't load sound file from asset catalog. Verify file is a valid sound file and that the name is correct.")
    }
}

// Thanks!

Gallaugher
  • 1,593
  • 16
  • 27

1 Answers1

2

Declaring the AVAudioPlayer is important so that the object is retained while the sound is playing, so it is a best practice to declare the player as a property as it looks like you have done.

I see your goal in wanting to defer learning about optionals until later, so I see what you're trying to accomplish by setting the player to an empty player.

I might suggest declaring the player as implicitly optional:

var audioPlayer: AVAudioPlayer!

Implicitly optional variables do not require unwrapping - great when you know a variable will not be nil before use, but can't be initialized in an init method.

You could tell your students to not worry about the '!' for now, and it keeps the use of audioPlayer the same as you have it in your code sample. (In fact, most IBOutlets are declared this way when implementing UI)

It's not a best practice to set the player to an empty player because you are allocating memory for an object that never gets used.

Hope that helps!

John B.
  • 36
  • 1
  • Here is a great post that goes over implicit optionals in more detail: http://stackoverflow.com/questions/24006975/why-create-implicitly-unwrapped-optionals – John B. Sep 16 '16 at 22:48
  • Thanks so much. All this is very helpful. And thanks for confirming the approach is ok! – Gallaugher Sep 17 '16 at 13:01