Source of problem is that init?(named name: String)
return same NSSound
instance for same name. You can copy NSSound
instance, then several sounds will play simultaneously:
function keyDownSound(){
NSSound(named: "tennis")?.copy().play()
}
Alternative way - start sound again after finish playing. For that you need to implement sound(sound: NSSound, didFinishPlaying aBool: Bool)
delegate method. For example:
var sound: NSSound?
var playCount: UInt = 0
func playSoundIfNeeded() {
if playCount > 0 {
if sound == nil {
sound = NSSound(named: "Blow")!
sound?.delegate = self
}
if sound?.playing == false {
playCount -= 1
sound?.play()
}
}
}
func keyDownSound() {
playCount += 1
playSoundIfNeeded()
}
func sound(sound: NSSound, didFinishPlaying aBool: Bool) {
playSoundIfNeeded()
}