Play MP3 or WAV file via the Linux command line

I would like to make an alarm system backed by a Ubuntu (no graphical interface) box, which plays various announcement and alarm audio tracks (.mp3 or .wav) via the command line.

For example:

$ root> audioplay ./hello.wav

The audio should come from the PC audio jack. I might also wrap it with another socket listener (for example, Ruby Sinatra).

How can I do this?

1

10 Answers

mpg123 is a command-line utility which plays mp3 files. You can install it in Ubuntu with:

sudo apt-get install mpg123
1

The play command from the sox package will play any file format supported by sox using the default audio device, e.g

$ play something.mp3
$ play something.wav

You may need to install extra packages to gain support for all formats, for example on Ubuntu 11.04 the MP3 support is not available until you install libsox-fmt-mp3.

3

The most standard way to play a WAV file in Linux is using the aplay command, which is part of the ALSA system.

aplay [flags] [filename [filename]] ...
aplay a.wav

Links: (Wikipedia) (aplay man page)

(Both in Fedora and in Ubuntu/Mint it is part of the alsa-utils package)

This does not require any additional packages to your Linux installation like sox or mplayer or vlc, just the basic ALSA which is a part of any system nowadays.

7

Install vlc by using:

sudo apt-get install vlc vlc-plugin-pulse mozilla-plugin-vlc

Make sure that you have all repositories open. Also run the following before you install:

sudo apt-get update

VLC has a command-line operation method invoked by cvlc. The next part would be to write a .sh that will call the command. I am no good at writing bash scripts. The end-result would be something like:

cvlc xyz.mp3
cvlc --play-and-exit done.mp3 
4

You can simply pipe your sound data to the pc speaker device:

cat rawsound | /dev/pcsp
5

On Ubuntu 16.04 (Xenial Xerus), there is no need to install anything. You can play a sound using paplay [audio] with is part of the PulseAudio sound server:

paplay mysound.mp3
2

MPlayer is another player which can play pretty much any audio/video format from command line. To install it in Ubuntu just execute this command:

sudo apt-get install mplayer

You can then play the file using this syntax:

mplayer [path to file]

I found another way:

FFmpeg is installed on my Ubuntu 19.04 (Disco Dingo)

So:

$ ffplay music.mp3
-nodisp
hide spectrum analyzer
-nostats
hide cursor/file information
-hide_banner
hide build information
Hide all (no output):
$ ffplay music.mp3 -nodisp -nostats -hide_banner
1

canberra-gtk-play

For simple Bash scripts MPlayer is probably a bit too heavy and too verbose in terms of output. A built-in option is canberra-gtk-play which comes preinstalled on Ubuntu:

canberra-gtk-play --file=/usr/share/sounds/gnome/default/alerts/drip.ogg

Note: it uses the alerts volume, and you must pass --file= in order to play a file from a path.

It can also play a sound by id which represents the file name without extension of media files under /usr/share/sounds (apparently this only works for sounds that are registered as part of a sound theme):

canberra-gtk-play --id="desktop-login"
canberra-gtk-play --id="message"

gst-launch-1.0/gst-launch-0.10

Another option is using the gstreamer command-line tools which are present on most modern Linux boxes:

gst-launch-1.0 playbin uri=file:///usr/share/sounds/ubuntu/stereo/message.ogg

To suppress all output redirect it to /dev/null:

gst-launch-1.0 playbin uri=file:///usr/share/sounds/ubuntu/stereo/message.ogg > /dev/null 2>&1

Both gst-launch-1.0 and gst-launch-0.10 might be present on your system.

You can play all sound files (mp3, wav, ogg etc) via ffplay

ffplay -nodisp -autoexit ticktock.mp3

ffmpeg is very powerful and you can steam it to other sockets as well as sound cards.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like