110

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?

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
c2h2
  • 2,363
  • 3
  • 18
  • 18

10 Answers10

93

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.

82

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.

Maxim
  • 1,556
  • 12
  • 13
  • 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 – Jesse Hallett Dec 06 '12 at 01:35
  • 2
    Yes, Jesse! I do exactly the same with compiling (long file conversions, etc). I use sounds from here: ["Opilki sounds"](http://addons.miranda-im.org/details.php?action=viewfile&id=3379) (they are under the Creatve Commons license) forgive me this minor advertisement, i'm not related to the project in any way :) – Maxim Dec 07 '12 at 07:39
  • 2
    Dead easy and already installed everywhere. +1 – Pitto Jul 26 '13 at 09:08
  • 11
    Remember that you **can't** play an MP3 with `aplay`. You'll just get static. – starbeamrainbowlabs Jul 25 '16 at 20:03
  • 1
    @starbeamrainbowlabs, i wrote about WAV files only ! – Maxim Aug 08 '16 at 02:01
  • 2
    @Maxim I know! I just added that comment because I tried to play an mp3 :P – starbeamrainbowlabs Aug 08 '16 at 05:39
  • 2
    This is a correct answer. yeah you can install too many CLI multimedia players buy why not use a built-in one. – pouya Apr 17 '20 at 11:50
58

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

sudo apt-get install mpg123
Gaff
  • 18,569
  • 15
  • 57
  • 68
pavium
  • 6,412
  • 1
  • 27
  • 27
26

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 
Michael Cole
  • 717
  • 6
  • 8
prometheuspk
  • 361
  • 2
  • 4
12

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

cat rawsound | /dev/pcsp
troelskn
  • 411
  • 2
  • 10
10

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
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
mxdsp
  • 240
  • 3
  • 9
  • 1
    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? – Peter Hansen Feb 12 '20 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 09 '20 at 12:53
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]
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Ali Parsai
  • 81
  • 1
  • 2
6

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
Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Gromish
  • 61
  • 1
  • 2
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.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
ccpizza
  • 7,456
  • 6
  • 54
  • 56
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.