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)

  • Runtime error when using MPMediaPickerController in iOS Simulator
  • Using Alamofire and multipart/form-data
  • Share image/text through WhatsApp in an iOS app
  • Does @synchronized guarantees for thread safety or not?
  • Can't update calabash server version
  • Navigation Bar Title Font Size
  • 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")
            }
        }