Sign in to follow this  
Followers 0
Guest jazo

can pressing a keystroke stop a sound playing?

4 posts in this topic

i have a script playing a sound file with the soundplay function with a flag on 1.

i was wondering if you could put something in the script that if the user pushes a mix or keystroke it will stop playing?

for example Ctrl + S?

any help would be grand! :D

Share this post


Link to post
Share on other sites



You couldn't do it with flag 1 unless you spawned a 2nd script and had the first one terminate the process of the 2nd.

However, with flag 0, you could manage to stop a sound, and here's how I would go about it. Have a command line paramater for your script that spawns a 2nd instance that plays the sound with flag 0. For the durration of the sound, have it check to see if a window title exists that would contain a message from your first script to stop. If this is encountered, exit the 2nd instance, which will stop the sound from playing. Here's an example.

If $CmdLine[0] = 2 Then
  SoundPlay($CmdLine[1])
  For $i = 1 To $CmdLine[2] ;number of seconds to wait
    Sleep(1000)
    If WinExists("Sound:Exit Script") Then Exit ;exit if we told it to
  Next
  Exit ;prevent 2nd spawned instance from going into our main script
EndIf

$Song = "C:\My_song.mp3"
$time = 60 ;how many seconds long the song is

HotKeySet("^s", "EndSong") ;enable hotkey
Run(@ScriptFullPath & '"' $Song & '" ' & $time) ;play song with 2nd instance
For $i = 0 To 65 ;65 seconds of sleep so that my example doesn't terminate early
  Sleep(1000)
Next

Func EndSong()
  AutoItWinSetTitle("Sound:Exit Script") ;signal 2nd instance to exit
  Sleep(1500) ;give instance time to exit
  AutoItWinSetTitle("AutoIt v3") ;reset signal title
EndFunc

Another solution would be to launch mplayer2 in a hidden window, and have it play your song. Just have the hotkey exit the program. The only disadvantage I can see to this is that it's not a full AutoIt approach, so if someone managed to get rid of mplayer2.exe on thier comp, this would fail.


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

yeah thanks for that.....

i sort of did something similar my self,

HotKeySet("^!x", "MyExit")
SoundPlay("\\jason\share$\1.wma")
sleep(50000); the lenght of the song
Func MyExit()
    Exit
EndFunc

but thanks for your help any way

Share this post


Link to post
Share on other sites

Heh, glad you figured something similar out. Your method is simplier if you don't need to run more code after you stop playing. I wasn't aware you were willing to do that, so I developed an approach that could be implimented with additional code. And perhaps someone else will come along and this will be just what they needed :D


[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0