Linux Commands

The aplay Linux Command

Aplay is a great option for playing audio files on the command line. It is a tool for Advanced Linux Sound Architecture (ALSA) sound card drivers. Aplay supports various sound cards and file formats on multiple devices, and it works similar to how a recorder does, only that in this case, it plays the audio instead of recording it.

Aplay determines the bit rate, sample rate, file formats, and all other details from the sound file header before playing the audio. Let’s see some of the examples of using aplay in Linux.

How to Play an Audio with Aplay

To use aplay, pass the name of the audio file and any options to tweak your preference. The basic syntax is as follows:

$ aplay [options] [audiofile]

There are many options available to use with the aplay command, and you can open the help page to see which option to use.

To view the options available, use the following command:

$ aplay -h

List the Available Sound Cards and Digital Audio Devices

Different machines have different hardware devices. You can view the playback hardware devices available for your device by using the -l flag as expressed in the following example:

$ aplay -l

Your output will be different from the following output due to the difference in architecture.

List All PCMs

You can view the Pulse Code Modulation available for your device using the -L flag.

Take a look at the following command:

$ aplay -L

Play the Audio on Command Line

The aplay utility allows you to play audio files via the command line. If you wonder how that works, take a look at the following case. We have an audio file named aplaysample.mp3. To play the audio without specifying other options, run the following command:

$ aplay aplaysample.mp3

The audio will start playing, and aplay will use the default settings based on the audio details to play it.

However, you can specify how you want the file to play. Let’s have some examples:

1. Play the Audio for a Specified Time and Frequency

Let’s play the audio for 20 seconds at a given frequency of 3500Hz.

The command will be:

$ aplay -d 20 -r 3500 aplaysample.mp3

Here, the -d flag specifies the duration to play the audio and the -r specifies the frequency. The same specifications are reflected on the following output:

Once the set time elapses, the audio stops and you can go ahead and choose other settings.

2. Play the Full Audio at a Set Frequency

You don’t need to use the –d flag to play the audio at full length. For instance, we can set to play the full audio at 2500 Hz using the following command:

$ aplay -r 2500 aplaysample.mp3

3. Suppress the Messages

If you don’t want any messages to display on the terminal as you play an audio using aplay, use the -q flag.

4. Select the File Type

Aplay supports different file types such as voc, au, raw, and wav. The default format, if no format is specified, is WAVE.

In the following example, we will specify the file type wav using the given command:

$ aplay -t wav aplaywavsample

5. Non-Blocking Mode

Aplay can be opened in a non-block mode. If the device is busy, it will exit. If the device is free, it will play your file.

To set the non-block mode, add the -nonblock flag or -N.

$ aplay -t wav -N aplaywavsample

How to Interrupt Aplay While Playing An Audio

By default, aplay will play the audio until the duration elapses. However, we’ve seen how you can set the time using the -d flag. To forcefully interrupt, type ctrl + c.

You will see an interrupt message once you kill the program.

Conclusion

I hope you now understand how you can use the aplay Linux command to play different audio format files on your command line. In this guide, we have seen the various examples of using various options with aplay to make your experience better. Aplay is a great utility. You should try it out.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.