Using AVAudioPlayer to play sounds in iOS applications

In iOS applications, there are scenarios where we need to play media sounds (like mp3) in iOS applications. In this tutorial, we will discuss how to play sound files added to XCode using AVAudioPlayer in the iOS app.

The AVAudioPlayer object is used to play audio data from a file or buffer. The AVAudioPlayer plays data from a file or buffer. It is declared as follows.

We can use the AVAudioPlayer to play audio of any duration from a file or buffer. We can also control the volume, rate, panning, and looping behavior of the played audio. It plays multiple sounds simultaneously by synchronizing the playback of multiple players.

The AVAudioPlayer provides the following methods to control the playback behavior.

SNMethodDescription
1func prepareToPlay() -> BoolThis function is used to prepare the audio playback
2func play() -> BoolThis function is used to play the audio asynchronously.
3func play(atTime: TimeInterval) -> BoolThis function schedules the playtime at the given time interval.
4func pause()This function is used to pause the playback.
5func stop()This function is used to stop the audio playback.

Let's create a project which plays the sound file added to the project.

First, we need to add the AVFoundation framework to our project. For this purpose, go to the General tab in XCode project properties and tap the "+" icon in the framework and libraries section given at the bottom. We will get the dialogue box to choose from the frameworks. Type AVFoundation in the box as shown below to add the framework.

Using AVAudioPlayer to play sounds in iOS applications

For this project, we will add a sample mp3 or wav sound file. Once we have the sound file, add it to our project by dragging and dropping it into XCode, as shown below.

Using AVAudioPlayer to play sounds in iOS applications

In the ViewController class, we need to import the AVFoundation framework to use the framework classes.

Now, let's create the AVAudioPlayer class object in the ViewController class.

As we have created AVAudioPlayer() object now, we need to read the sound file we have just added to the XCode. For this purpose, add the following code in ViewController.

If we notice that we have used Bundle in the above code to find the Sound file named as Sound of type mp3.

Now, we will play sound using the play () method of AVAudioPlayer, as shown below.

Let's add the play button in the Main.storyboard by clicking which, we can play the sound in our iOS app.

Using AVAudioPlayer to play sounds in iOS applications

Now create the action outlet of the play button in the ViewController and add the following code.

Now, we will notice once we run the app on an iOS device and tap on the play button, the audio will be played.

The AVAudioPlayer class provides several other properties to control the playback of the audio player. Let's look at the properties of the AVAudioPlayer class.

SNPropertyDescription
1var isPlaying: BoolIt is the Boolean value indicating whether the audio player is being played or not.
2var volume: FloatIt is the float value representing the volume level of the Audio Player relative to other audio output.
3var pan: FloatIt represents the pan position of the audio player.
4var enableRate: BoolIt is the Boolean value that indicates whether we can adjust the playback rate of the audio player.
5var rate: FloatIt represents the audio player?s playback rate.
6var numberOfLoops: IntIt represents the number of times the audio repeats playback.
7var currentTime: TimeIntervalIt represents the playback time, in seconds, within the audio timeline.
8var duration: TimeIntervalIt represents the total duration of the audio playback.
9var numberOfChannels: IntIt represents the number of audio channels in the player?s audio
10var channelAssignments: [AVAudioSessionChannelDescription]?It is the array of channel descriptions associated with the audio player.
11var isMeteringEnabled: BoolIt is a Boolean value indicating whether we have enabled the audio player to enable the metering data.
12var url: URL?It represents the URL of the audio file.
13var data: Data?It represents the audio binary data.
14var format: AVAudioFormatIt represents the format of the player?s audio data.
15var settings: [String : Any]It is a dictionary that provides information about the player's audio data.
16var currentDevice: String?It is a unique identifier for the current audio player.
17var deviceCurrentTime: TimeIntervalIt is the current time value in seconds shown in the audio output device?s clock.





Latest Courses