Jump to content

how to change any program volume by it handle


Recommended Posts

good morning autoit team

please i need your help

am trying to make an program that work with hotkeys

it function is to change the volume to the current application

e.g when i select the vlc window by using this hotkey i will be able to change the vlc volume with out changing the system volume.

 am able to get the process handle and the process name of the window

but when am trying to get or set the volume i does not succeded

i tried to search in this forums but no result.

can you please guide me to do that.

am waiting for your answers

thanks in advanced

Link to comment
Share on other sites

..had a hard time understanding the title. Do change it to "how to change any program's sound volume by it handle".
and sorry but I have no clue of how to do that. ( But pressing up key or down key while in VLC it'll go sound volume up or down. )

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Edited by careca
Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

Had this code saved from somewhere in the forum, made some tweaks to allow volume to be changed with the keyboard arrows.

The only thing you have to give is a processname. Adapt to your needs.

#include-once
;https://www.autoitscript.com/forum/topic/174460-is-there-a-way-to-detect-any-sound-file-played/
;Special Thanks to Danyfirex 02/08/2015
#include <WinAPIProc.au3>
#include <Array.au3>
#include <Misc.au3>


Global Const $sCLSID_MMDeviceEnumerator = "{BCDE0395-E52F-467C-8E3D-C4579291692E}"
Global Const $sIID_IMMDeviceEnumerator = "{A95664D2-9614-4F35-A746-DE8DB63617E6}"
Global Const $sTagIMMDeviceEnumerator = _
        "EnumAudioEndpoints hresult(int;dword;ptr*);" & _
        "GetDefaultAudioEndpoint hresult(int;int;ptr*);" & _
        "GetDevice hresult(wstr;ptr*);" & _
        "RegisterEndpointNotificationCallback hresult(ptr);" & _
        "UnregisterEndpointNotificationCallback hresult(ptr)"

Global Const $sIID_IMMDevice = "{D666063F-1587-4E43-81F1-B948E807363F}"
Global Const $sTagIMMDevice = _
        "Activate hresult(struct*;dword;ptr;ptr*);" & _
        "OpenPropertyStore hresult(dword;ptr*);" & _
        "GetId hresult(wstr*);" & _
        "GetState hresult(dword*)"

Global Const $sIID_IAudioSessionManager2 = "{77aa99a0-1bd6-484f-8bc7-2c654c9a9b6f}"
Global Const $sTagIAudioSessionManager = "GetAudioSessionControl hresult(ptr;dword;ptr*);" & _
        "GetSimpleAudioVolume hresult(ptr;dword;ptr*);"
Global Const $sTagIAudioSessionManager2 = $sTagIAudioSessionManager & "GetSessionEnumerator hresult(ptr*);" & _
        "RegisterSessionNotification hresult(ptr);" & _
        "UnregisterSessionNotification hresult(ptr);" & _
        "RegisterDuckNotification hresult(wstr;ptr);" & _
        "UnregisterDuckNotification hresult(ptr)"

Global Const $sIID_IAudioSessionEnumerator = "{e2f5bb11-0570-40ca-acdd-3aa01277dee8}"
Global Const $sTagIAudioSessionEnumerator = "GetCount hresult(int*);GetSession hresult(int;ptr*)"

Global Const $sIID_IAudioSessionControl = "{f4b1a599-7266-4319-a8ca-e70acb11e8cd}"
Global Const $sTagIAudioSessionControl = "GetState hresult(int*);GetDisplayName hresult(ptr);" & _
        "SetDisplayName hresult(wstr);GetIconPath hresult(ptr);" & _
        "SetIconPath hresult(wstr;ptr);GetGroupingParam hresult(ptr*);" & _
        "SetGroupingParam hresult(ptr;ptr);RegisterAudioSessionNotification hresult(ptr);" & _
        "UnregisterAudioSessionNotification hresult(ptr);"

Global Const $sIID_IAudioSessionControl2 = "{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}"
Global Const $sTagIAudioSessionControl2 = $sTagIAudioSessionControl & "GetSessionIdentifier hresult(ptr)" & _
        "GetSessionInstanceIdentifier hresult(ptr);" & _
        "GetProcessId hresult(dword*);IsSystemSoundsSession hresult();" & _
        "SetDuckingPreferences hresult(bool);"

; http://answers.awesomium.com/questions/3398/controlling-the-sound-using-pinvoke-the-volume-mix.html
Global Const $sIID_ISimpleAudioVolume = "{87CE5498-68D6-44E5-9215-6DA47EF883D8}"
Global Const $sTagISimpleAudioVolume = _
        "SetMasterVolume hresult(float;ptr);" & _
        "GetMasterVolume hresult(float*);" & _
        "SetMute hresult(int;ptr);" & _
        "GetMute hresult(int*)"
Global $__oIAudioSessionManager2

Local $PList = ProcessList()
_ArrayDisplay($PList)
$SoundAPP = InputBox('Process name', 'What is the process name?', 'firefox.exe')
Local $CurrentVolume = Audio_GetMasterVolume(_GetProcessID_by_name($SoundAPP)) * 100
Local $Vol = $CurrentVolume
MsgBox(64 + 262144, $Vol, $SoundAPP)
Local $SMV
While 1
If _IsPressed(26) Then
    ConsoleWrite('UP! ' &$Vol&@CRLF)
        $Vol = $Vol+1
        Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $Vol/ 100)
        ConsoleWrite('$SMV:'& $SMV&@CRLF)
        Sleep(10)
EndIf
If _IsPressed(28) Then
    ConsoleWrite('DOWN! ' &$Vol&@CRLF)
        $Vol = $Vol-1
        $SMV=Audio_SetMasterVolume(_GetProcessID_by_name($SoundAPP), $Vol/ 100)
        ConsoleWrite('$SMV:'& $SMV&@CRLF)
        Sleep(10)
EndIf
$CurrentVolume = Round(Audio_GetMasterVolume(_GetProcessID_by_name($SoundAPP)) * 100, 0)
ConsoleWrite('CV:'&$CurrentVolume &@CRLF)
Sleep(100)
WEnd

Func _GetProcessID_by_name($process_name)
    Local $Process_list = ProcessList()
    Local $ProcessID_Func = 0
    For $i = 1 To UBound($Process_list) - 1
        If $Process_list[$i][0] = $process_name Then
            $ProcessID_Func = $Process_list[$i][1]
            ExitLoop
        EndIf
    Next
Return  $ProcessID_Func
EndFunc   ;==>_SoundAPP_setVolume

Func Audio_SetMasterVolume($pid, $value)
    If Not IsObj($__oIAudioSessionManager2) Then
        $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2()
    EndIf
    If Not IsObj($__oIAudioSessionManager2) Then Return

    Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator
    If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return
    $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
    If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1)

    Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2
    Local $ProcessID, $oISimpleAudioVolume
    Local $bMute = 0, $error = 1
    If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then
        For $i = 0 To $nSessions - 1
            If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then
                $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
                If @error Then ContinueLoop
                $oIAudioSessionControl2.GetProcessId($ProcessID)
                If $ProcessID = $pid Then
                    $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume)
                    If @error Then ContinueLoop
                    $oIAudioSessionControl2.AddRef() ;stabilize
                    If $oISimpleAudioVolume.SetMasterVolume($value, 0) >= 0 Then
                        $error = 0
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    $oISimpleAudioVolume = 0
    $oIAudioSessionControl2 = 0
    $oIAudioSessionEnumerator = 0
    ;MsgBox(0, $error, "App muted: " & $bMute)
    Return SetError($error, 0, $bMute)
EndFunc   ;==>Audio_SetMasterVolume

Func Audio_GetMasterVolume($pid)
    If Not IsObj($__oIAudioSessionManager2) Then
        $__oIAudioSessionManager2 = Audio_GetIAudioSessionManager2()
    EndIf
    If Not IsObj($__oIAudioSessionManager2) Then Return

    Local $pIAudioSessionEnumerator, $oIAudioSessionEnumerator
    If $__oIAudioSessionManager2.GetSessionEnumerator($pIAudioSessionEnumerator) < 0 Then Return
    $oIAudioSessionEnumerator = ObjCreateInterface($pIAudioSessionEnumerator, $sIID_IAudioSessionEnumerator, $sTagIAudioSessionEnumerator)
    If Not IsObj($oIAudioSessionEnumerator) Then Return SetError(1)

    Local $i, $nSessions, $pIAudioSessionControl2, $oIAudioSessionControl2
    Local $ProcessID, $oISimpleAudioVolume
    Local $bMute = 0, $error = 1
    If $oIAudioSessionEnumerator.GetCount($nSessions) >= 0 Then
        For $i = 0 To $nSessions - 1
            If $oIAudioSessionEnumerator.GetSession($i, $pIAudioSessionControl2) >= 0 Then
                $oIAudioSessionControl2 = ObjCreateInterface($pIAudioSessionControl2, $sIID_IAudioSessionControl2, $sTagIAudioSessionControl2)
                If @error Then ContinueLoop
                $oIAudioSessionControl2.GetProcessId($ProcessID)
                If $ProcessID = $pid Then
                    $oISimpleAudioVolume = ObjCreateInterface($pIAudioSessionControl2, $sIID_ISimpleAudioVolume, $sTagISimpleAudioVolume)
                    If @error Then ContinueLoop
                    $oIAudioSessionControl2.AddRef() ;stabilize
                    If $oISimpleAudioVolume.GetMasterVolume($bMute) >= 0 Then
                        $error = 0
                        ExitLoop
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    $oISimpleAudioVolume = 0
    $oIAudioSessionControl2 = 0
    $oIAudioSessionEnumerator = 0
    ;MsgBox(0, $error, "App muted: " & $bMute)
    Return SetError($error, 0, $bMute)
EndFunc   ;==>Audio_GetMasterVolume

Func Audio_GetIAudioSessionManager2()
    Local $oIAudioSessionManager2 = 0
    Local Const $eMultimedia = 1, $CLSCTX_INPROC_SERVER = 0x01
    Local $pIMMDevice, $oMMDevice, $pIAudioSessionManager2
    Local $oMMDeviceEnumerator = ObjCreateInterface($sCLSID_MMDeviceEnumerator, $sIID_IMMDeviceEnumerator, $sTagIMMDeviceEnumerator)
    If IsObj($oMMDeviceEnumerator) Then
        If $oMMDeviceEnumerator.GetDefaultAudioEndpoint(0, $eMultimedia, $pIMMDevice) >= 0 Then
            $oMMDevice = ObjCreateInterface($pIMMDevice, $sIID_IMMDevice, $sTagIMMDevice)
            If IsObj($oMMDevice) Then
                If $oMMDevice.Activate(__uuidof($sIID_IAudioSessionManager2), $CLSCTX_INPROC_SERVER, 0, $pIAudioSessionManager2) >= 0 Then
                    $oIAudioSessionManager2 = ObjCreateInterface($pIAudioSessionManager2, $sIID_IAudioSessionManager2, $sTagIAudioSessionManager2)
                EndIf
                $oMMDevice = 0
            EndIf
        EndIf
        $oMMDeviceEnumerator = 0
    EndIf

    If IsObj($oIAudioSessionManager2) Then
        Return $oIAudioSessionManager2
    EndIf
EndFunc   ;==>Audio_GetIAudioSessionManager2

Func __uuidof($sGUID)
    Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
    DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
    If @error Then Return SetError(@error, @extended, 0)
    Return $tGUID
EndFunc   ;==>__uuidof

Func CLSIDFromString($sGUID)
    Local $tGUID = DllStructCreate("ulong Data1;ushort Data2;ushort Data3;byte Data4[8]")
    DllCall("ole32.dll", "long", "CLSIDFromString", "wstr", $sGUID, "struct*", $tGUID)
    Return $tGUID
EndFunc   ;==>CLSIDFromString

 

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

in the inputbox you have to put one processname that is currently running, which one did you put in, if any? The default is firefox.exe, do you have firefox running?

What is the process name of the application you want to change the volume of?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
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
 Share

×
×
  • Create New...