[SOLVED] How to stop 'aplay' from playing a song?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

[SOLVED] How to stop 'aplay' from playing a song?

#1 Post by Argolance »

Hello,
All is in the tittle...
Thank you!

Best regards!
Last edited by Argolance on Sat 01 Oct 2011, 17:37, edited 1 time in total.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#2 Post by don570 »

Does "killall aplay" work in terminal?

I personally use my task manager to stop unwanted music.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#3 Post by Argolance »

Hello,
Thank you for answering.
I need, inside a script, user to test sound files: start and eventually stop playing them before they end. If I kill aplay I kill other playing processes too. It is not what I want.
Strange that this very basic command doesn't exist! :shock:

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#4 Post by don570 »

I noticed that it is possible to kill a gtkdialog process by
its name. I believe I used that in a script I recently wrote
(probably quick_launch)

I grepped for the gtkdialog name to get the process number.
and then killed the process number.

So I wonder if you could arrange each song
to have a separate gtkdialog name??

or somehow open the song with another application so
that the song showed up on the list of processes.

For instance I discovered that if I had my text editor
open to edit a script and then did 'ps' in terminal
that the name of the script would appear in the output
which was undesirable for me, but maybe you could
use that to help you spot the song and kill it.

_______________________________________

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#5 Post by Argolance »

Hello,
=> Found this!
... Will try tomorow!!!

Good night.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

Argolance,

Either -

aplay <somefilename> &
PID=$!
kill $PID

-or-

aplay -s <somefilename> (already running)

EDIT: I tried both of these and this works-

Code: Select all

 
exec aplay <somefilename> &
PID=$!
sleep 2
kill $PID
Apparently the pup aplay doesn't have an -s option.


Not sure which is better,

Cheers,
s

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#7 Post by Argolance »

Hello,
Thank you.
I have got to buttons, one to start (<action>aplay \$MYSOUNDFILE &</action>, one to stop playing (<action>???????</action>: Sorry but I don't see how to transpose your code lines:

Code: Select all

PID=$!

Code: Select all

kill $PID
... inside my script.

Regards!

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#8 Post by seaside »

Argolance wrote:Hello,
Thank you.
I have got to buttons, one to start (<action>aplay \$MYSOUNDFILE &</action>, one to stop playing (<action>???????</action>: Sorry but I don't see how to transpose your code lines:

Code: Select all

PID=$!

Code: Select all

kill $PID
... inside my script.

Regards!
Argolance,

Perhaps this -

Code: Select all

play() {
aplay <soundfile> & 
PID=$! # can't see var PID, so needs -
echo $PID >/tmp/playtmp
}
export -f play

export MAIN_DIALOG='
<hbox>
    <button>
    <label>Play</label>
            <action>play</action>
    </button>
    <button>
    <label>Stop</label>
            <action>kill `cat /tmp/playtmp`</action>
     </button>
 </hbox>'
 gtkdialog3 --program=MAIN_DIALOG   


Regards,
s

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#9 Post by Argolance »

Hello seaside,
I adapted and tried code lines above! Something very strange happens: when I run my script in console, the displayed process number is different from the one that is saved in the file /tmp/playtmp (23642)... :shock:
Playing WAVE '/usr/share/audio/login/00_current' : Signed 16 bit Little Endian, Rate 22050 Hz, Stereo #Play button
sh: line 0: kill: (28245) - Aucun processus de ce type (no process of this type) #Stop button!
Playing WAVE '/usr/share/audio/login/00_current' : Signed 16 bit Little Endian, Rate 22050 Hz, Stereo #Play button... again
sh: line 0: kill: (28245) - Aucun processus de ce type #Stop button... again
Script completed hit RETURN to close window.
What's wrong?
Thanks!

Cordialement.

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#10 Post by vovchik »

Dear puppians,

This works as a toggle, and you can link it to rox (customize menu), so that when you click on a sound file, it will play or, by clicking on the same or another sound file of the same type, it will kill aplay:

Code: Select all

#!/bin/sh

mybin="aplay"
myfile="$@"

if [ "$(pidof $mybin)" ]; then
	killall -KILL aplay
else
	$mybin "$myfile" > /dev/null 2>&1&
fi
exit 0
With kind regards,
vovchik

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#11 Post by Argolance »

Hello,
Thank you to all!
@vovchik,
Your proposition is right! Perhaps, some explanations should be welcome, please... (<pidof>? </dev/null 2>&1&>?

@seaside
I used your function to make it run inside my script and this is quite perfect:

Code: Select all

play() {
mybin="aplay"
myfile="$@"

if [ "$(pidof $mybin)" ]; then
   killall -KILL aplay
else
   $mybin "$myfile" > /dev/null 2>&1&
fi
}
export -f play
 
export MAIN_DIALOG="
<hbox>
    <button> <input file stock=\"gtk-media-play\"></input>
    <label>Start playing</label>
            <action>play</action>
    </button>
    <button> <input file stock=\"gtk-media-stop\"></input>
    <label>Stop playing</label>
            <action>play</action>
     </button>
 </hbox>"
 gtkdialog3 --program=MAIN_DIALOG
Best regards

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#12 Post by vovchik »

Dear Argolance,

I am glad you got it working. I see one little infelicity in my own code:

The bit:

Code: Select all

if [ "$(pidof $mybin)" ]; then
   killall -KILL aplay
should actually read:

Code: Select all

if [ "$(pidof $mybin)" ]; then
   killall -KILL $mybin
just so you can use this code for various on/off things and just change the contents of the variable $mybin.

Explanations: "pidof" is a unix command that returns the PID of an executable that is in memory, making it easy to find and kill processes. If a process is found (e.g. "aplay"), the condition is true and the "killall" routine is called. Otherwise it is not.

The bit:

Code: Select all

/dev/null 2>&1& 
simply redirects all stdout and stdin to the "bitbucket" so as not to show garbage in the terminal. The last "&" means execute a command as a separate process.

With kind regards,
vovchik

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#13 Post by seaside »

Argolance,

I tried the code in a console and it worked, so I'm not sure what's happening.

Since Vovchik has very kindly provided another solution using a toggle, you could even eliminate one of the buttons.

Code: Select all

<button image-position=\"top\" relief=\"2\" tooltip-text=\"Toggle to start or stop sound \">
    <input file>/mnt/sdb6/start_stop_button2.png</input>
    <label>Start or stop playing</label>
            <action>play</action>
    </button>
You may have to buy a BMW car to match the button :)

Regards,
s
Attachments
start_stop.png
(33.28 KiB) Downloaded 1783 times

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#14 Post by vovchik »

Dear seaside,

Thanks for posting that image. I made a reasonably nice button icon out of it, and I think it might be useful for this and other little apps.

With kind regards,
vovchik
Attachments
start_stop.png
(25.92 KiB) Downloaded 1915 times

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#15 Post by seaside »

vovchik wrote:Dear seaside,

Thanks for posting that image. I made a reasonably nice button icon out of it, and I think it might be useful for this and other little apps.

With kind regards,
vovchik
Vovchik,

Nice looking and handy button.

Not a single bit of infelicity there :)

Regards,
s

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#16 Post by Argolance »

Hello,
@vovchik
Thank you a lot for explanations: rather rod for fishing than fish to eat! :wink:
@seaside
Since Vovchik has very kindly provided another solution using a toggle, you could even eliminate one of the buttons.
Yes I did but as I have got other gtk stock buttons inside my script, this beautiful fat one :D is not appropriate...
So, I made a 'gtk stock like' button, for those who are interested in!

Regards.
Attachments
start_stop.png
(891 Bytes) Downloaded 234 times
start_stop_button_script.jpg
(13.52 KiB) Downloaded 249 times

Post Reply