The aplay Linux Command
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:
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:
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:
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:
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:
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:
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:
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:
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.
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.
Source: linuxhint.com