Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,539
304
Hi guys

I was fumbling around with a project. I tried to implement a dictionary but for some reason I'm not getting any sound (the sound file is where it ought to be). This is a Model class. Can somebody explain me what is going wrong?

Code:
private var guitarPlayer = AVAudioPlayer()

    private var applausePlayer = AVAudioPlayer()

    private var monsterPlayer = AVAudioPlayer()

    private var bubblesPlayer = AVAudioPlayer()

   

    private let soundDictionary: [String: AVAudioPlayer]

   

    init(){

        soundDictionary = ["guitar": guitarPlayer, "applause": applausePlayer, "monster": monsterPlayer, "bubbles": bubblesPlayer]

    }

   

    func play(sound: String){


        var player = soundDictionary[sound]!

        if let url = Bundle.main.url(forResource: sound, withExtension: "wav"){

            player = try! AVAudioPlayer(contentsOf: url)

            player.play()

        }

    }

   

    func playGuitarSound()  {

//        if let url = Bundle.main.url(forResource: "guitar", withExtension: "wav"){

//            guitarPlayer = try? AVAudioPlayer(contentsOf: url)

//            guitarPlayer?.play()

//        }

        self.play(sound: "guitar")

    }

Tx once more
ps: what also bothers me is that I had to declare soundDictionary as a [String: AVAudioPlayer]. I would have preferred [String: AVAudioPlayer?] but then I got, of course, nil on the statement
Code:
var player = soundDictionary[sound]!
 
Did you debug this? Did you step through the code in the debugger? Add print() statements to see the code path?
 
First, don't create 3 instances of AVAudioPlayer. Just create one and reuse it. Feed the single instance different audio files.

Second, why are you force unwrapping soundDictionary when it isn't classified as an Optional?

Third, are you sure that you have you URL's set up properly? Does that URL even exist?

Set a break point and check if the URLs are nil.
 
What do you think the lifetime of your player object is?

I think the answer is about three lines of code, and that's the problem. Your player object is being dealloced immediately.

The way you've written the code with initializing blank AVAudioPlayer objects and then building a new one every time you want to play a sound indicates only a partial understanding of how memory management works in swift.
 
Last edited:
First, don't create 3 instances of AVAudioPlayer. Just create one and reuse it. Feed the single instance different audio files.

Second, why are you force unwrapping soundDictionary when it isn't classified as an Optional?

Third, are you sure that you have you URL's set up properly? Does that URL even exist?

Set a break point and check if the URLs are nil.
hmm the unwrapping makes no sense indeed. The URL's weren't empty.
Thanks for your advice.
[doublepost=1479930580][/doublepost]
What do you think the lifetime of your player object is?

I think the answer is about three lines of code, and that's the problem. Your player object is being dealloced immediately.

The way you've written the code with initializing blank AVAudioPlayer objects and then building a new one every time you want to play a sound indicates only a partial understanding of how memory management works in swift.
Shouldn't the player play at least once when the func play() is called? I agree that the pointer to the object is deallocated afterwards.
 
hmm the unwrapping makes no sense indeed. The URL's weren't empty.
Thanks for your advice.
[doublepost=1479930580][/doublepost]
Shouldn't the player play at least once when the func play() is called? I agree that the pointer to the object is deallocated afterwards.

Just because the URL's aren't empty doesn't mean they contain the correct path to the file. I'm guessing this is your issue.
 
Try another app (a Music player, etc.) to make sure that sound works as expected on your device. Then download some of Apple's example code that uses the same sound API, build and run that on your device. Then look at what's different between Apple's code which works, and your code which doesn't.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.