adamcichy / SwiftySound

SwiftySound is a simple library that lets you play sounds with a single line of code.
MIT License
1.24k stars 98 forks source link

After the 5th time playing a sound doesn't start from 0:00. #59

Open VotusX opened 3 years ago

VotusX commented 3 years ago

Hello,

I was running into the issue that after I play a sound for the fifth time by clicking a button using the Sound.play(file: "X.mp3") method, it doesn't start from it's very beginning and just seems to play at a random point.

Best regards

SuperWomble commented 3 years ago

Looking at the code, there's a weird design issue where 5 instances of AVAudioPlayer are created for every sound. Despite the code comments, I'm not really sure why this is done.

six8 commented 3 years ago

5 instances are queued up so you can quickly play the same sound up to 5 times simultaneous. For example, a bullet sound from a machine gun.

adamcichy commented 2 years ago

You can switch to just one player per sound like this:

Sound.playersPerSound = 1

This way you will always use the same instance of AVAudioPlayer for each sound.