Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

Creating and playing a sound in swift (where I started originally)

And these videos:

https://www.youtube.com/watch?v=Kq7eVJ6RSp8

https://www.youtube.com/watch?v=RKfe7xzHEZk

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.

like image 676
Q.H. Avatar asked Aug 04 '16 15:08

Q.H.


1 Answers

UPDATE/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")
        }
    }
like image 133
Q.H. Avatar answered Sep 20 '22 23:09

Q.H.