The reason the audio stops is because you only have one AVAudioPlayer, so when you ask the class to play another sound, you are currently replacing the old instance with a new instance of AVAudioPlayer. You rewrite it basically.
You can create two instances of the GSAudio class, and then call playSound for each of them, or make the class a universal audio manager that uses a dictionary of audio planners.
I prefer the latter option because it allows you to use cleaner code and is also more efficient. You can check if you really made a player for sound, instead of creating a new player, for example.
In any case, I remade my class for you to play several sounds at once. It can also play the same sound over itself (it does not replace the previous sound instance) Hope this helps!
The class is singleton, so to access the class, use:
GSAudio.sharedInstance
For example, to play the sound you are calling:
GSAudio.sharedInstance.playSound("AudioFileName")
and play several sounds at once:
GSAudio.sharedInstance.playSounds("AudioFileName1", "AudioFileName2")
or you can load sounds in an array somewhere and call the playSounds function, which takes an array:
let sounds = ["AudioFileName1", "AudioFileName2"] GSAudio.sharedInstance.playSounds(sounds)
I also added the playSounds function, which allows you to delay every sound played in cascading format. So:
let soundFileNames = ["SoundFileName1", "SoundFileName2", "SoundFileName3"] GSAudio.sharedInstance.playSounds(soundFileNames, withDelay: 1.0)
will play sound2 a second after sound1, then sound3 will play a second after sound2, etc.
Here is the class:
class GSAudio: NSObject, AVAudioPlayerDelegate { static let sharedInstance = GSAudio() private override init() {} var players = [NSURL:AVAudioPlayer]() var duplicatePlayers = [AVAudioPlayer]() func playSound (soundFileName: String){ let soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!) if let player = players[soundFileNameURL] {