13

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 &?

andrew.46
  • 39,359
Ant's
  • 3,930

6 Answers6

14

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 by kill $! (if you haven't backgrounded other processes in the same terminal after starting play -- $! gives you the PID of the last backgrounded process)

ish
  • 141,990
4

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

tomodachi
  • 15,370
1

This worked for me with mplayer

nohup mplayer  SomeSong.mp4 > /dev/null 2>&1 &
brandon
  • 11
  • 2
0

My Nautilus-Actions to add "Play" and "Stop" to the context menu for audio files

To Add Play...

Define a new action as follows

"Action Tab"

  1. Checkbox Ticked for "Display item in selection context menu"
  2. New Action Context Label= Play
  3. Tooltip= Play Audio
  4. Icon= media-playback-start

"Command Tab"

  1. Label= Play
  2. Path= /usr/bin/play -q
  3. Parameters= %f
  4. (Optional W/Effects) Parameters= %f rate -v vol .25 bass +5 treble +5 reverb
  5. Working Directory= %d

"Execution Tab"

  1. Checkbox Ticked for "Normal"

"Mimetypes Tab"

  1. Add audio/mpeg
  2. Add audio/flac
  3. Add audio/x-wav

Or add more if needed or leave it alone to show up everywhere.

The rest can be left alone.


To Add Stop...

Define a new action as follows

"Action Tab"

  1. Checkbox Ticked for "Display item in selection context menu"
  2. Context Label= Stop
  3. Tooltip= Stop Audio
  4. Icon= media-playback-stop

"Command Tab"

  1. Label= Stop
  2. Path= killall
  3. Parameters= play
  4. Working Directory= %d

"Execution Tab"

  1. Checkbox Ticked for "Normal"

"Mimetypes Tab"

  1. Add audio/mpeg
  2. Add audio/flac
  3. Add audio/x-wav

Or add more if needed or leave it alone to show up everywhere.

The rest can be left alone.

deleted
  • 2,619
0

I use

cvlc --play-and-exit "$filename" &>/dev/null &

vlc is pretty ubiquitous.

glenn jackman
  • 18,218
0

There are many players in the market. You can try open source which is opensource and cross platform. It is mplayer. Try this command to check it is either there or not.

mplayer --help 
man mplayer

You will see the manual or documentation of this command. You can play you the audio file. Check your drivers. You should have drivers before use it. If you don't this player install it with apt command in Ubuntu 16.04 LTS or apt-get command.

sudo apt install mplayer

or

sudo apt-get install mplayer

use it simply by typing mplayer and file name after that.

mplayer file_path
muru
  • 207,228