Jump to content
Sign in to follow this  
memerim

Stop sound if win active

Recommended Posts

Writing a script that detect the PID who executed the autoit script (thanks to @AdamUL for the help in this part) and based on the parent process name who executed it, play a determined wav sound (doing this way because all programs have same tittle and class name, the only way i could detect the program who launched the script was reading her process name, ex: 1.exe, 2.exe, 3.exe)

I have a folder in C:\audios\ which contains a lot of other folders, with different wav files (C:\audios\1\, C:\audios\2\, etc).

What im trying is after execute the wav file, if i active the window wich same process name than the window who launched the autoit script, stop the sound being played.

My main goal with this script is to avoid multiple process playing different wav files at same time, and stopping the audio in the same moment i active the window who started it.

#include <Process.au3>
   #include <WinAPIProc.au3>
   #include <AutoItConstants.au3>
   #include <MsgBoxConstants.au3>
   #include <MISC.au3>


   ProcessWaitClose("alarm.exe")
   
   Global $iScriptParentPID = _WinAPI_GetParentProcess(@AutoItPID)
   Global $sParentProcessName = _ProcessGetName($iScriptParentPID)


   If $sParentProcessName = "1.exe" Then

   SoundPlay("C:\audios\1\expired.wav", 0)

   Endif

   If $sParentProcessName = "2.exe" Then

   SoundPlay("C:\audios\2\expired.wav", 0)

   Endif

   If $sParentProcessName = "3.exe" Then

   SoundPlay("C:\audios\3\expired.wav", 0)

   Endif

   If $sParentProcessName = "4.exe" Then

   SoundPlay("C:\audios\4\expired.wav", 0)

   Endif

   If $sParentProcessName = "5.exe" Then

   SoundPlay("C:\audios\5\expired.wav", 0)

   Endif

   If $sParentProcessName = "6.exe" Then

   SoundPlay("C:\audios\6\expired.wav", 0)

   Endif

   If $sParentProcessName = "7.exe" Then

   SoundPlay("C:\audios\7\expired.wav", 0)

   Endif

   If $sParentProcessName = "8.exe" Then

   SoundPlay("C:\audios\8\expired.wav", 0)

   Endif

   If $sParentProcessName = "9.exe" Then

   SoundPlay("C:\audios\9\expired.wav", 0)

   Endif

   If $sParentProcessName = "10.exe" Then

   SoundPlay("C:\audios\10\expired.wav", 0)

   Endif

   If $sParentProcessName = "11.exe" Then

   SoundPlay("C:\audios\11\expired.wav", 0)

   Endif

   If $sParentProcessName = "12.exe" Then

   SoundPlay("C:\audios\12\expired.wav", 0)

   Endif

   If $sParentProcessName = "13.exe" Then

   SoundPlay("C:\audios\13\expired.wav", 0)

   Endif

   If $sParentProcessName = "14.exe" Then

   SoundPlay("C:\audios\14\expired.wav", 0)

   Endif

   If $sParentProcessName = "15.exe" Then

   SoundPlay("C:\audios\15\expired.wav", 0)

   Endif

   If $sParentProcessName = "16.exe" Then

   SoundPlay("C:\audios\16\expired.wav", 0)

   Endif

   If $sParentProcessName = "17.exe" Then

   SoundPlay("C:\audios\17\expired.wav", 0)

   Endif

   
   Local $Actwin = WinGetHandle("[ACTIVE]")
   Local $PidActwin = WinGetProcess($Actwin)
   Local $NamePidActwin = _Findpidname($PidActwin)


   If $NamePidActwin = $sParentProcessName Then

   SoundPlay("")

   EndIf


   Func _Findpidname($Pid)
       Local $Processlist = ProcessList()
       For $i = 1 To $Processlist[0][0]
           If $Processlist[$i][1] = $Pid Then Return $Processlist[$i][0]
       Next
    EndFunc   ;==>_Findpidname

 

  Could be possible optimize this part to avoid adding many lines for each process number? The number on exe file will be always the number in audio folder

If $sParentProcessName = " .exe" Then

   SoundPlay("C:\audios\( )\expired.wav", 0)

   Endif

   I already did the code to compare the active window pid which the pid of the process who started the wav file, then if match stop the sound, im struggle in how i could loop this part, while the sound being played, and exit it, if the sound stop before?

Local $Actwin = WinGetHandle("[ACTIVE]")
   Local $PidActwin = WinGetProcess($Actwin)
   Local $NamePidActwin = _Findpidname($PidActwin)


   If $NamePidActwin = $sParentProcessName Then

   SoundPlay("")

   EndIf

 

Edited by memerim

Share this post


Link to post
Share on other sites

 

53 minutes ago, memerim said:

Could be possible optimize this part to avoid adding many lines for each process number?

Try something like this (untested) - then just call that function with the parent process as the parameter.  Check the UDF for _SoundStatus to get the status of a sound and also _soundPlay and _soundStop.

func _playSound($aParentProcessName)
    SoundPlay("C:\audios\"&StringTrimRight($aParentProcessName,4)&"\expired.wav")
EndFunc
Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

This should stop scripts playing sounds upon activating a folder that titles [title & .exe] = @ScriptName

While 1
    Sleep(1000)
    $sTitle = WinGetTitle(WinGetHandle("", ""), "")
    If @ScriptName = $sTitle & ".exe" Then
        SoundPlay("")
        Exit
    EndIf
WEnd

Deye

Edited by Deye

Share this post


Link to post
Share on other sites

JFish and Deye, thank you both, using your examples i optimized my code:

#RequireAdmin
   #include <Process.au3>
   #include <WinAPIProc.au3>
   #include <AutoItConstants.au3>
   #include <MsgBoxConstants.au3>
   #include <MISC.au3>

   ;MsgBox(0, "Process Name", $sParentProcessName) ;For testing.?
   ;ProcessWaitClose("audio.exe")

   Global $iScriptParentPID = _WinAPI_GetParentProcess(@AutoItPID)
   Global $sParentProcessName = _ProcessGetName($iScriptParentPID)

   SoundPlay("C:\audios\"&StringTrimRight($sParentProcessName,4)&"\expired.wav"")


   While 1

      $Actwin = WinGetHandle("[ACTIVE]")
      $PidActwin = WinGetProcess($Actwin)
      $NamePidActwin = _Findpidname($PidActwin)
      Sleep(200)

      If $NamePidActwin = $sParentProcessName Then
      SoundPlay("")
      Exit
      EndIf

   WEnd


   Func _Findpidname($Pid)
       Local $Processlist = ProcessList()
       For $i = 1 To $Processlist[0][0]
           If $Processlist[$i][1] = $Pid Then Return $Processlist[$i][0]
       Next
    EndFunc   ;==>_Findpidname

The script stop playing the audio as i active the process window who started the script.

There's any option to make thread solved here in the forum?

Share this post


Link to post
Share on other sites
2 hours ago, memerim said:

There's any option to make thread solved here in the forum?

1

The way that I have always done it is to just edit your first post. You should be able to edit the title there.


Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 

Share this post


Link to post
Share on other sites

-Edit- :doh:

I compile a script with the name "audio.exe" and inside the script script he checks for:

If ProcessExists("audio.exe") Then

      Endif

Is possible ignore herself in this function, and only succeed if have another "audio.exe" opened?

Edited by memerim

Share this post


Link to post
Share on other sites

Try using the _Singleton function to determine if there's an instance of it already running, unless you need more than 2 of them running at the same time it should work.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

@BrewManNH

Using singleton worked:

If _Singleton("audio", 1) = 0 Then
          Else
          ExitLoop
      EndIf

With that code the script is able to detect if any other compiled exe is running, the problem is, i have this script running in different sandboxies and inside a sandboxie it dont detect others instances, each sandboxie have a username different, reading on singleton help file:

Quote

You can place the object in a namespace by prefixing your object name with either "Global\" or "Local\". "Global\" objects combined with the flag 2 are useful in multi-user environments.

So i did:

Global $name = "audio"

     If _Singleton($name, 2) = 0 Then
          Else
          ExitLoop
      EndIf

Still not detecting others audio.exe running at same time.

How _singleton detect another instance?

 

-----EDIT-----

I solved it, was a conflict with sandboxie settings, sorry.

Edited by memerim

Share this post


Link to post
Share on other sites

Try this:

If _Singleton("Global\audio", 2) = 0 Then
          Else
          ExitLoop
      EndIf

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...