71

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. (e.g. Ruby Sinatra)

Any ideas how I can do this?

migrated from stackoverflow.com Apr 28 '11 at 16:55

This question came from our site for professional and enthusiast programmers.

  • 3
    So what is your question? – Quentin Apr 27 '11 at 13:01
  • 1
    Just in case you're looking for example files: /usr/share/sounds is a good place to look for them. – Martin Thoma Sep 16 '14 at 11:37
46

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

sudo apt-get install mpg123
  • 5
    This doesn't play wav files. – Cerin Jun 14 '17 at 3:31
62

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.

  • 1
    it also supports ogg files (and probably others). – lepe Jun 3 '15 at 6:12
  • 1
    In Ubuntu Xenial, there is libsox-fmt-all package to install all formats. – Evgeniy Philippov Aug 6 '17 at 19:23
49

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)

(In Fedora it is part of the alsa-utils, and in Ubuntu the package likely has the same name.)

This does not require any additional packages like sox or mplayer or vlc, just ALSA.

  • 5
    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 – Jesse Hallett Dec 6 '12 at 1:35
  • 1
    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 '12 at 7:39
  • Dead easy and already installed everywhere. +1 – Pitto Jul 26 '13 at 9:08
  • 3
    Remember that you can't play an MP3 with aplay. You'll just get static. – starbeamrainbowlabs Jul 25 '16 at 20:03
  • @starbeamrainbowlabs, i wrote about WAV files only ! – Maxim Aug 8 '16 at 2:01
19

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 
  • 1
    cvlc too slow to boot up? and need to run by a non-root user? – c2h2 May 9 '11 at 3:24
  • 1
    cvlc --play-and-exit done.mp3 if you don't want to ctrl-c it. – Michael Cole Nov 5 '15 at 20:23
  • 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) – loved.by.Jesus Apr 18 '18 at 8:46
7

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

cat rawsound | /dev/pcsp
  • 1
    I very much doubt that is going to work with MP3 files. – user89061 Jul 9 '11 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 '11 at 8:05
  • 3
    I don't have the /dev/pcsp device. What else can I try? – trusktr Aug 31 '13 at 5:21
  • I get "cat: rawsound: Datei oder Verzeichnis nicht gefunden" – Timo Apr 8 '15 at 7:04
7

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]

good luck!

4

On Ubuntu 16.04, 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
0

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.