Why is AVAudioPlayer not making any sound?

Discussion in 'iOS Programming' started by grandM, Nov 15, 2016.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    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]!
     
  2. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    No that is not the problem. I was using the simulator and the commented out code used to work.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Did you debug this? Did you step through the code in the debugger? Add print() statements to see the code path?
     
  5. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #5
    Yes I did. I used the debugger and all seemed fine. I'll debug it again to make sure.
     
  6. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #6
    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.
     
  7. PhoneyDeveloper, Nov 17, 2016
    Last edited: Nov 18, 2016

    PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    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.
     
  8. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    hmm the unwrapping makes no sense indeed. The URL's weren't empty.
    Thanks for your advice.
    --- Post Merged, Nov 23, 2016 ---
    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.
     
  9. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #9
    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.
     
  10. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #10
    I don't think so. The commented out code use to work.
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    You tell me?

    Just for fun why not make 'var player' a property instead of a local variable and see if it makes any difference.
     
  12. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #12
    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.
     

Share This Page