How can I play a song in the background via my command line?
Solution 1
Backgrounding play
with &
fails because play
wants to output its status, e.g.
In:12.7% 00:00:27.31 [00:03:07.52] Out:1.20M [!=====|=====!] Hd:0.0 Clip:0
but cannot if backgrounded. So it keeps waiting until aborted.
To solve this, simply run play
with the -q
(quiet) switch. This will successfully background it and play
will terminate when the song ends.
(sleep 10 ; play -q Desktop/SONGS/01\ -\ Oh\ Baby\ Girl.mp3 ) &
You can stop it by either typing
killall play
(if no other play instances are running), or bykill $!
(if you haven't backgrounded other processes in the same terminal after starting play -- $! gives you the PID of the last backgrounded process)
Solution 2
there is a better way of running things in the "background" from the command line.
sudo apt-get install tmux
Its one of the most nifty command line programs for Linux. it allows you to have something similar to "tabs in a browser" and switching seamlessly between them without any interruption of running programs. It's not tabs in the terminal program itself though. But within that specific shell you have started.
once install start it by typing
tmux
in a terminal.
you create new "tabs" with ctrl-A c you switch to next tab with ctrl-A n
here is a more thorough tutorial if you find the man pages hard to understand
Solution 3
This worked for me with mplayer
nohup mplayer SomeSong.mp4 > /dev/null 2>&1 &
Related videos on Youtube
Ant's
Updated on September 18, 2022Comments
-
Ant's over 1 year
I have installed
play
:sudo apt-get install sox libsox-fmt-mp3
I can now play my audio files like this :
play Desktop/SONGS/01\ -\ Oh\ Baby\ Girl.mp3
Since I'm learning shell, I wish I could do something like this :
(sleep 10 ; play Desktop/SONGS/01\ -\ Oh\ Baby\ Girl.mp3 ) &
After
10
sec's, I can see the screen as :File Size: 7.38M Bit Rate: 260k Encoding: MPEG audio Info: 2012 Channels: 2 @ 16-bit Track: 01/09 Samplerate: 44100Hz Album: Maalai Pozhudhin Mayakathilaey :::tunesinn.blogspot.com::: Replaygain: off Artist: Hemachandra, Achu Duration: 00:03:46.98 Title: Oh Baby Girl
But the song is not playing. But if I do this (without
&
) :(sleep 10 ; play Desktop/SONGS/01\ -\ Oh\ Baby\ Girl.mp3 )
Is working as expected. But I couldn't able to use my terminal in meanwhile.
How could I resolve my problem, with using
&
?-
Dyde almost 12 yearsIf you only want a CLI player so that you can play audio in the background, I would highly recommend you to try
cmus
. It even has a scriptcmuscrobbler
for last.fm and you can map global hotkeys in X window system viacmus-remote
. Or you could try using an utility likescreen
.
-
-
Ant's almost 12 yearsThis works. But how come now I stop playing the song?
-
ish almost 12 yearsYou can stop it by either typing
killall play
(if no other play instances are running), or bykill $!
(if you haven't backgrounded other processes in the same terminal after starting play --$!
gives you the PID of the last backgrounded process) -
Kathy001 almost 11 yearsAdjust as needed. Play with the effects options, see the sox manpage for more on that... manpages.ubuntu.com/manpages/raring/en/man1/sox.1.html
-
gertvdijk almost 11 yearsHow does this answer the question regarding running this from the command line? Your answer seems to be about Nautilus, a GUI-enabled file manager. It's a nice answer, but for a different question.
-
Kathy001 almost 11 yearsBecause its a quick and cool way to do this even though its not a command line answer. Couldn't find a question to fit the answer in this case.