AVAudioPlayer not playing audio Swift

I am attempting to play a sound as an alert to the user of my app and I’ve looked at a few sources in trying to help me do this:

AVAudioPlayer not playing audio in Swift (to help me solve the problem I am running into now, to no avail)

  • How to Submit an iOS app to the App Store (iTunes Connect) using Xcode
  • Add icon to home screen like Safari
  • Getting error incorrect password on google console in firebase
  • How to use pull to refresh in Swift?
  • Send POST parameters with MultipartFormData using Alamofire, in iOS Swift
  • What's the difference between float and double?
  • Creating and playing a sound in swift (where I started originally)

    And these videos:

    All of which are not giving me the desired outcome (the sound doesn’t play).

    Here is my code:

    private func playFinishedSound(){
            if let pathResource = NSBundle.mainBundle().pathForResource("3000", ofType: "mp3"){
                let finishedStepSound = NSURL(fileURLWithPath: pathResource)
                var audioPlayer = AVAudioPlayer()
                do {
                    audioPlayer = try AVAudioPlayer(contentsOfURL: finishedStepSound)
                    if(audioPlayer.prepareToPlay()){
                        print("preparation success")
                        audioPlayer.delegate = self
                        if(audioPlayer.play()){
                            print("Sound play success")
                        }else{
                            print("Sound file could not be played")
                        }
                    }else{
                        print("preparation failure")
                    }
    
                }catch{
                    print("Sound file could not be found")
                }
            }else{
                print("path not found")
            }
        }
    

    Currently I see “preparation success” and “sound play success” but no sound is played. The class I am implementing this in is an AVAudioPlayerDelegate and the file is called “3000.mp3” which is within the project directory. In context the method is called here:

    private func finishCell(cell: TimerTableViewCell, currentTimer: TimerObject){
            currentTimer.isRunning = false
            cell.label.text = "dismiss"
            cell.backgroundColor = UIColor.lightMintColor()
            if(!currentTimer.launchedNotification){
                playFinishedSound()
            }
            currentTimer.launchedNotification = true
        }
    

    Any help would be appreciated.

    Solutions Collect From Internet About “AVAudioPlayer not playing audio Swift”

    UPADTE/SOLUTION:

    So the problem was that audioPlayer would become deallocated before it would play the sound, to fix this I had to make it a property within the class instead of just creating an instance of it within the function. The updated code looks like this:

    Optional reference within the property declarations of the class:

    var audioPlayer : AVAudioPlayer?
    

    The function that utilizes the audioPlayer:

    private func playFinishedSound(){
            if let pathResource = NSBundle.mainBundle().pathForResource("3000", ofType: "mp3"){
                let finishedStepSound = NSURL(fileURLWithPath: pathResource)
                audioPlayer = AVAudioPlayer()
                do {
                    audioPlayer = try AVAudioPlayer(contentsOfURL: finishedStepSound)
                    if(audioPlayer!.prepareToPlay()){
                        print("preparation success")
                        audioPlayer!.delegate = self
                        if(audioPlayer!.play()){
                            print("Sound play success")
                        }else{
                            print("Sound file could not be played")
                        }
                    }else{
                        print("preparation failure")
                    }
    
                }catch{
                    print("Sound file could not be found")
                }
            }else{
                print("path not found")
            }
        }