114

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
  • 2
    Just in case you're looking for example files: /usr/share/sounds is a good place to look for them. Sep 16, 2014 at 11:37

10 Answers 10

58

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

sudo apt-get install mpg123
1
  • 15
    This doesn't play wav files.
    – Cerin
    Jun 14, 2017 at 3:31
100

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
  • 1
    it also supports ogg files (and probably others).
    – lepe
    Jun 3, 2015 at 6:12
  • 2
    In Ubuntu Xenial, there is libsox-fmt-all package to install all formats. Aug 6, 2017 at 19:23
  • BTW, the -v option specify the volume, e.g 1 is normal, 0.5 is half, 2 is twice.
    – Eric
    Mar 15, 2020 at 11:56
85

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
  • 9
    Thanks for the tip! I added this alias to my shell config: alias beep="aplay --quiet /usr/share/sounds/pop.wav" . That way I can get a notification when long running commands finish. For example: compile && run && beep Dec 6, 2012 at 1:35
  • 2
    Yes, Jesse! I do exactly the same with compiling (long file conversions, etc). I use sounds from here: "Opilki sounds" (they are under the Creatve Commons license) forgive me this minor advertisement, i'm not related to the project in any way :)
    – Maxim
    Dec 7, 2012 at 7:39
  • 2
    Dead easy and already installed everywhere. +1
    – Pitto
    Jul 26, 2013 at 9:08
  • 12
    Remember that you can't play an MP3 with aplay. You'll just get static. Jul 25, 2016 at 20:03
  • 2
    @Maxim I know! I just added that comment because I tried to play an mp3 :P Aug 8, 2016 at 5:39
27

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
  • 2
    cvlc too slow to boot up? and need to run by a non-root user?
    – c2h2
    May 9, 2011 at 3:24
  • 2
    cvlc --play-and-exit done.mp3 if you don't want to ctrl-c it. Nov 5, 2015 at 20:23
  • 1
    cvlc --play-and-exit --no-loop done.mp3 . I need the extra option --no-loop, so that the sound file is not repeated over and over. (vlc 2.2.2, xubuntu 16.04.4) Apr 18, 2018 at 8:46
  • note that vlc usually depends on qt5 and X on some distribution, but mpg123 depends on only alsa-lib.
    – recolic
    Oct 2, 2019 at 18:25
13

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
3
  • 2
    I would think this works only if paplay --list-file-formats includes MP3 in the format list, which it does not on my machine (which, admittedly, is not Ubuntu). Does paplay ever really include MP3? Feb 12, 2020 at 14:42
  • Correct. WAV seems to be on that list but not MP3. You might need to install proprietary codecs. On Ubuntu that would be sudo apt-get install ubuntu-restricted-extras.
    – mxdsp
    Mar 9, 2020 at 12:53
  • This actually worked for mp3 files and it was on my system all along :o
    – Jore
    Sep 6, 2023 at 23:02
12

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

cat rawsound | /dev/pcsp
5
  • 1
    I very much doubt that is going to work with MP3 files.
    – user89061
    Jul 9, 2011 at 12:11
  • 2
    It won't work with mp3 encoded sound of course, but it will work with raw wav data
    – troelskn
    Jul 18, 2011 at 8:05
  • 7
    I don't have the /dev/pcsp device. What else can I try?
    – trusktr
    Aug 31, 2013 at 5:21
  • I get "cat: rawsound: Datei oder Verzeichnis nicht gefunden"
    – Timo
    Apr 8, 2015 at 7:04
  • 1
    This is so beautifully simple. I love it. Jul 8, 2020 at 3:08
8

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]
7

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
  • Is there a way to use the interactive commands with stdin and -nodisp?
    – Avrumy
    Dec 11, 2020 at 4:10
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.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .