Javatpoint Logo
Javatpoint Logo

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.

SN Method Description
1 func prepareToPlay() -> Bool This function is used to prepare the audio playback
2 func play() -> Bool This function is used to play the audio asynchronously.
3 func play(atTime: TimeInterval) -> Bool This function schedules the playtime at the given time interval.
4 func pause() This function is used to pause the playback.
5 func 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.

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

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA