|
-
May 25th, 2025, 06:37 AM
#1
[VB6, twinBASIC] Core Audio: Mute/unmute another process
This is a simple code snippet for two functions: MuteProcess and IsProcessMuted, going by process id.
Windows 7+, x86/x64 compatible.
Requirements (IDE only)
VB6: oleexp.tlb w/ addons (inc. w/ download) mIID.bas and mCoreAudio.bas
twinBASIC: Windows Development Library for twinBASIC (WinDevLib) (References->Available packages)
Code
Code:
Private Function MuteProcess(ByVal pid As Long, ByVal bMute As Boolean, Optional pdwError As Long) As Boolean
On Error GoTo e0
Dim mDefRenderMM As IMMDevice
Dim mDeviceEnum As MMDeviceEnumerator
Dim pSessionMgr As IAudioSessionManager2
Dim pEnum As IAudioSessionEnumerator
Dim pControl As IAudioSessionControl
Dim pControl2 As IAudioSessionControl2
Dim pVol As ISimpleAudioVolume
Set mDeviceEnum = New MMDeviceEnumerator
mDeviceEnum.GetDefaultAudioEndpoint eRender, eConsole, mDefRenderMM
If (mDefRenderMM Is Nothing) = False Then
mDefRenderMM.Activate IID_IAudioSessionManager2, CLSCTX_INPROC_SERVER, CVar(0), pSessionMgr
End If
If (pSessionMgr Is Nothing) = False Then
Set pEnum = pSessionMgr.GetSessionEnumerator()
Dim nSessions As Long
pEnum.GetCount nSessions
If nSessions Then
Dim i As Long
For i = 0 To nSessions - 1
pEnum.GetSession i, pControl
Set pControl2 = pControl
Dim spid As Long
pControl2.GetProcessId spid
If spid = pid Then
Debug.Print "MuteProcess::Found audio session for pid " & pid
Set pVol = pControl2
Dim fMute As BOOL
fMute = IIf(bMute, 1, 0)
pVol.SetMute bMute, UUID_NULL
Exit Function
End If
Debug.Print "MuteProcess::Enumerated audio sessions but none found for pid " & pid
pdwError = 1
Next
Else
Debug.Print "MuteProcess::No per-app audio sessions found."
pdwError = 2
End If
Else
Debug.Print "MuteProcess::Couldn't activate audio session manager."
pdwError = 3
End If
Exit Function
e0:
Debug.Print "MuteProcess::An unexpected error occurred, 0x" & Hex$(Err.Number) & ": " & Err.Description
pdwError = Err.Number
End Function
Private Function IsProcessMuted(ByVal pid As Long, pdwError As Long) As Boolean
On Error GoTo e0
Dim mDefRenderMM As IMMDevice
Dim mDeviceEnum As MMDeviceEnumerator
Dim pSessionMgr As IAudioSessionManager2
Dim pEnum As IAudioSessionEnumerator
Dim pControl As IAudioSessionControl
Dim pControl2 As IAudioSessionControl2
Dim pVol As ISimpleAudioVolume
Set mDeviceEnum = New MMDeviceEnumerator
mDeviceEnum.GetDefaultAudioEndpoint eRender, eConsole, mDefRenderMM
If (mDefRenderMM Is Nothing) = False Then
mDefRenderMM.Activate IID_IAudioSessionManager2, CLSCTX_INPROC_SERVER, CVar(0), pSessionMgr
End If
If (pSessionMgr Is Nothing) = False Then
Set pEnum = pSessionMgr.GetSessionEnumerator()
Dim nSessions As Long
pEnum.GetCount nSessions
If nSessions Then
Dim i As Long
For i = 0 To nSessions - 1
pEnum.GetSession i, pControl
Set pControl2 = pControl
Dim spid As Long
pControl2.GetProcessId spid
If spid = pid Then
Debug.Print "IsProcessMuted::Found audio session for pid " & pid
Set pVol = pControl2
Dim fMute As BOOL
pVol.GetMute fMute
IsProcessMuted = (fMute <> 0)
Exit Function
End If
Debug.Print "IsProcessMuted::Enumerated audio sessions but none found for pid " & pid
pdwError = 1
Next
Else
Debug.Print "IsProcessMuted::No per-app audio sessions found."
pdwError = 2
End If
Else
Debug.Print "IsProcessMuted::Couldn't activate audio session manager."
pdwError = 3
End If
Exit Function
e0:
Debug.Print "IsProcessMuted::An unexpected error occurred, 0x" & Hex$(Err.Number) & ": " & Err.Description
pdwError = Err.Number
End Function
The ISimpleAudioVolume interface also has GetMasterVolume/SetMasterVolume, and it would be trivial to adapt the code above to use those.
-
May 25th, 2025, 08:31 AM
#2
Re: [VB6, twinBASIC] Core Audio: Mute/unmute another process
Very nice although, I am trying to think about a real-life use being able to mute/unmute by process.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
May 25th, 2025, 09:19 AM
#3
Re: [VB6, twinBASIC] Core Audio: Mute/unmute another process
My sndvol.exe is broken so it was 30min to make a utility to replace it or hours of fighting with Windows probably ending in a 2-day reinstall. Or maybe you want to make a nicer one. Or automatically mute all apps except your own.
-
May 25th, 2025, 10:29 AM
#4
Re: [VB6, twinBASIC] Core Audio: Mute/unmute another process
 Originally Posted by fafalone
automatically mute all apps except your own.
Yep, I could use that, that is an interesting suggestion.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
May 25th, 2025, 06:05 PM
#5
Fanatic Member
Re: [VB6, twinBASIC] Core Audio: Mute/unmute another process
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|