目标是使可以播放 Flash 视频的 WebBrowser 控件静音。所以我发现这段代码非常有用:https ://stackoverflow.com/a/14322736/990618
问题是,当您期望来自 的某种 id 时,一些枚举是空值GetDisplayName
,我会得到 3-4 个空白和 2 个,就像“Mozilla Firefox”一样,"@%SystemRoot%\System32\AudioSrv.Dll,-202"
这是系统声音。
所以继续尝试GetProcessId、GetSessionIdentifier 和GetSessionInstanceIdentifier。
GetProcessId
将只返回零和一,GetSessionIdentifier
与 相同的结果GetDisplayName
,GetSessionInstanceIdentifier
所有空白。
为什么这些空白和零和一?
这是修改后的EnumerateApplications
:
public static IEnumerable<string> EnumerateApplications()
{
// get the speakers (1st render + multimedia) device
IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
IMMDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
// activate the session manager. we need the enumerator
Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o;
speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
// enumerate sessions for on this device
IAudioSessionEnumerator sessionEnumerator;
mgr.GetSessionEnumerator(out sessionEnumerator);
int count;
sessionEnumerator.GetCount(out count);
for (int i = 0; i < count; i++)
{
IAudioSessionControl ctl;
IAudioSessionControl2 ctl2;
sessionEnumerator.GetSession(i, out ctl);
ctl2 = ctl as IAudioSessionControl2;
string dn;
UInt32 pid = 0;
string sout = "";
if (ctl2 != null)
ctl2.GetSessionIdentifier(out sout);
//ctl.GetDisplayName(out dn);
// ctl2.GetProcessId(out pid);
//yield return pid.ToString();
yield return sout;
if (ctl != null)
Marshal.ReleaseComObject(ctl);
if (ctl2 != null)
Marshal.ReleaseComObject(ctl2);
}
Marshal.ReleaseComObject(sessionEnumerator);
Marshal.ReleaseComObject(mgr);
Marshal.ReleaseComObject(speakers);
Marshal.ReleaseComObject(deviceEnumerator);
}
[Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IAudioSessionControl2
{
[PreserveSig]
int GetProcessId([Out] [MarshalAs(UnmanagedType.U4)] out UInt32 processId);
[PreserveSig]
int GetSessionIdentifier([Out] [MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
[PreserveSig]
int GetSessionInstanceIdentifier([Out] [MarshalAs(UnmanagedType.LPWStr)] out string pRetVal);
}