play mp3 or wav file via Linux command line

81

20

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?

c2h2

Posted 2011-04-27T12:58:56.620

Reputation: 1 863

1Just in case you're looking for example files: /usr/share/sounds is a good place to look for them. – Martin Thoma – 2014-09-16T11:37:10.170

Answers

48

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

sudo apt-get install mpg123

pavium

Posted 2011-04-27T12:58:56.620

Reputation: 5 956

7This doesn't play wav files. – Cerin – 2017-06-14T03:31:39.293

74

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.

user89061

Posted 2011-04-27T12:58:56.620

Reputation:

1it also supports ogg files (and probably others). – lepe – 2015-06-03T06:12:09.587

1In Ubuntu Xenial, there is libsox-fmt-all package to install all formats. – Eugene Gr. Philippov – 2017-08-06T19:23:03.407

61

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

Posted 2011-04-27T12:58:56.620

Reputation: 1 107

4Remember that you can't play an MP3 with aplay. You'll just get static. – starbeamrainbowlabs – 2016-07-25T20:03:01.080

@starbeamrainbowlabs, i wrote about WAV files only ! – Maxim – 2016-08-08T02:01:55.323

@Maxim I know! I just added that comment because I tried to play an mp3 :P – starbeamrainbowlabs – 2016-08-08T05:39:47.223

5Thanks 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 – 2012-12-06T01:35:58.343

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 – 2012-12-07T07:39:52.297

Dead easy and already installed everywhere.

+1 – Pitto – 2013-07-26T09:08:26.867

23

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 

prometheuspk

Posted 2011-04-27T12:58:56.620

Reputation: 331

1cvlc --play-and-exit done.mp3 if you don't want to ctrl-c it. – Michael Cole – 2015-11-05T20:23:42.230

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 – 2018-04-18T08:46:26.690

note that vlc usually depends on qt5 and X on some distribution, but mpg123 depends on only alsa-lib. – recolic – 2019-10-02T18:25:33.650

1cvlc too slow to boot up? and need to run by a non-root user? – c2h2 – 2011-05-09T03:24:15.043

8

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

cat rawsound | /dev/pcsp

troelskn

Posted 2011-04-27T12:58:56.620

Reputation: 371

I get "cat: rawsound: Datei oder Verzeichnis nicht gefunden" – Timo – 2015-04-08T07:04:38.153

1I very much doubt that is going to work with MP3 files. – None – 2011-07-09T12:11:19.460

2It won't work with mp3 encoded sound of course, but it will work with raw wav data – troelskn – 2011-07-18T08:05:41.733

3I don't have the /dev/pcsp device. What else can I try? – trusktr – 2013-08-31T05:21:25.893

7

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

mxdsp

Posted 2011-04-27T12:58:56.620

Reputation: 220

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 – 2020-02-12T14:42:17.203

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!

Ali Parsai

Posted 2011-04-27T12:58:56.620

Reputation: 71

1

adding an answer for people that will pass here (question is 2011 old, my now is 2019), because I found another way:

ffmpeg is installed on my ubuntu 19.04

So:

$ ffplay music.mp3 

-nodisp
hide spectrum analyzer

-nostats
hide cursor/file informations

-hide_banner
hide build informations


hide all (no output):
$ ffplay music.mp3 -nodisp -nostats -hide_banner

enjoy

Gromish

Posted 2011-04-27T12:58:56.620

Reputation: 11

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.

ccpizza

Posted 2011-04-27T12:58:56.620

Reputation: 5 372