[SOLVED] How to stop 'aplay' from playing a song?
- 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?
Hello,
All is in the tittle...
Thank you!
Best regards!
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.
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.
_______________________________________
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.
_______________________________________
Argolance,
Either -
aplay <somefilename> &
PID=$!
kill $PID
-or-
aplay -s <somefilename> (already running)
EDIT: I tried both of these and this works-
Apparently the pup aplay doesn't have an -s option.
Not sure which is better,
Cheers,
s
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
Not sure which is better,
Cheers,
s
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
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:
... inside my script.
Regards!
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
Regards!
Argolance,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=$!
... inside my script.Code: Select all
kill $PID
Regards!
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
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
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)...
Thanks!
Cordialement.
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)...
What's wrong?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.
Thanks!
Cordialement.
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:
With kind regards,
vovchik
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
vovchik
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
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:Best regards
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
Dear Argolance,
I am glad you got it working. I see one little infelicity in my own code:
The bit:
should actually read:
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:
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
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
Code: Select all
if [ "$(pidof $mybin)" ]; then
killall -KILL $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&
With kind regards,
vovchik
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.
You may have to buy a BMW car to match the button
Regards,
s
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>
Regards,
s
- Attachments
-
- start_stop.png
- (33.28 KiB) Downloaded 1783 times
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
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
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Hello,
@vovchik
Thank you a lot for explanations: rather rod for fishing than fish to eat!
@seaside
So, I made a 'gtk stock like' button, for those who are interested in!
Regards.
@vovchik
Thank you a lot for explanations: rather rod for fishing than fish to eat!
@seaside
Yes I did but as I have got other gtk stock buttons inside my script, this beautiful fat one is not appropriate...Since Vovchik has very kindly provided another solution using a toggle, you could even eliminate one of the buttons.
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