Beispiel #1
0
        private static IAudioEndpointVolume GetMasterVolumeObject()
        {
            IMMDeviceEnumerator deviceEnumerator = null;
            IMmDevice           speakers         = null;

            try
            {
                deviceEnumerator = MmDeviceEnumeratorFactory.CreateInstance();
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out speakers);

                var    IID_IAudioEndpointVolume = typeof(IAudioEndpointVolume).GUID;
                object o;
                speakers.Activate(ref IID_IAudioEndpointVolume, 0, IntPtr.Zero, out o);
                IAudioEndpointVolume masterVol = (IAudioEndpointVolume)o;

                return(masterVol);
            }
            finally
            {
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }
Beispiel #2
0
        public static IEnumerable <IAudioSessionControl2> EnumerateApplications()
        {
            // get the speakers (1st render + multimedia) device
            var deviceEnumerator = MmDeviceEnumeratorFactory.CreateInstance();

            deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.ERender, ERole.EMultimedia, out IMmDevice speakers);

            // activate the session manager. we need the enumerator
            var iidIAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;

            speakers.Activate(ref iidIAudioSessionManager2, 0, IntPtr.Zero, out object o);
            var mgr = (IAudioSessionManager2)o;

            // enumerate sessions for on this device
            mgr.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnumerator);
            sessionEnumerator.GetCount(out int count);

            for (var i = 0; i < count; i++)
            {
                sessionEnumerator.GetSession(i, out IAudioSessionControl ctl);

                var ctl2 = ctl as IAudioSessionControl2;

                if (ctl2 != null)
                {
                    yield return(ctl2);
                }

                if (ctl != null)
                {
                    Marshal.ReleaseComObject(ctl);
                }

                if (ctl2 != null)
                {
                    Marshal.ReleaseComObject(ctl2);
                }
            }

            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
        }