Skip to content
DeveloperMemos

Playing a Sound Multiple Times with AVFoundation

AVFoundation, Swift, Sound2 min read

When it comes to playing sounds in an iOS app, AVFoundation is one of the most powerful and versatile frameworks available. It provides a wide range of functionalities that allow developers to easily work with different types of audio files and perform various tasks, such as playing, recording, and editing.

In this post, we will focus on a specific task: playing a sound multiple times using AVFoundation in iOS development using Swift.

Creating the AVAudioPlayer

The first step to playing a sound multiple times is to create an instance of AVAudioPlayer. This class provides a convenient way to play audio files, and it allows you to control the playback using different methods, such as play(), pause(), and stop().

Here's an example of how to create an instance of AVAudioPlayer:

1import AVFoundation
2
3class SoundPlayer {
4 var audioPlayer: AVAudioPlayer?
5
6 func playSound() {
7 guard let url = Bundle.main.url(forResource: "sound", withExtension: "mp3") else { return }
8 do {
9 audioPlayer = try AVAudioPlayer(contentsOf: url)
10 audioPlayer?.prepareToPlay()
11 audioPlayer?.play()
12 } catch let error {
13 print(error.localizedDescription)
14 }
15 }
16}

In this example, we create an instance of AVAudioPlayer and load the audio file "sound.mp3" from the app's main bundle using the url(forResource:withExtension:) method. We also prepare the player for playback by calling prepareToPlay(), and finally, we start playing the sound by calling the play() method.

Playing the Sound Multiple Times

Now that we have an instance of AVAudioPlayer, we can play the sound multiple times by calling the play() method repeatedly. However, there's a catch: if we call play() while the sound is still playing, nothing will happen. That's because the AVAudioPlayer instance is already playing, and it won't start playing again until it reaches the end of the audio file.

To play the sound multiple times, we need to reset the currentTime property of the AVAudioPlayer instance to zero before calling play(). This property specifies the current playback position, and resetting it to zero will start the sound from the beginning.

Here's an example of how to play a sound multiple times:

1class SoundPlayer {
2 var audioPlayer: AVAudioPlayer?
3
4 func playSoundMultipleTimes(count: Int) {
5 guard let url = Bundle.main.url(forResource: "sound", withExtension: "mp3") else { return }
6 do {
7 audioPlayer = try AVAudioPlayer(contentsOf: url)
8 audioPlayer?.numberOfLoops = count - 1
9 audioPlayer?.prepareToPlay()
10 audioPlayer?.play()
11 } catch let error {
12 print(error.localizedDescription)
13 }
14 }
15}

In this example, we create a new method called playSoundMultipleTimes(count:) that takes an integer parameter count representing the number of times we want to play the sound. We set the numberOfLoops property of the AVAudioPlayer instance to count - 1, which tells the player to repeat the sound count times. We then prepare the player for playback and start playing the sound by calling the play() method.

Closing Thoughts

In this post, we learned how to play a sound multiple times using AVFoundation in iOS development using Swift. We created an instance of AVAudioPlayer, loaded an audio file from the app's main bundle, and then used the play() method to play the sound multiple times by resetting the currentTime property of the player. We also saw how to use the numberOfLoops property to repeat the sound a specific number of times. AVFoundation provides a lot of powerful features for working with audio files, and with a little bit of knowledge and creativity, you can create some amazing audio experiences in your iOS apps.