Exemple #1
0
        private static ISimpleAudioVolume GetVolumeObject(string name)
        {
            IMMDeviceEnumerator devices = (IMMDeviceEnumerator) new MMDeviceEnumerator();
            IMMDevice           device  = devices.GetDefaultAudioEndpoint(EDATAFLOW_RENDER, EROLE_MULTIMEDIA);

            Guid sessionManagerGUID          = typeof(IAudioSessionManager2).GUID;
            IAudioSessionManager2   manager  = (IAudioSessionManager2)device.Activate(ref sessionManagerGUID, 0, IntPtr.Zero);
            IAudioSessionEnumerator sessions = manager.GetSessionEnumerator();

            ISimpleAudioVolume volumeObj = null;

            for (int index = sessions.GetCount() - 1; index >= 0; index--)
            {
                IAudioSessionControl2 ctl = sessions.GetSession(index) as IAudioSessionControl2;

                if (ctl != null)
                {
                    string identifier = ctl.GetSessionIdentifier();

                    if (identifier != null && identifier.Contains(name))
                    {
                        volumeObj = ctl as ISimpleAudioVolume;
                        break;
                    }

                    Marshal.ReleaseComObject(ctl);
                }
            }

            Marshal.ReleaseComObject(devices);
            Marshal.ReleaseComObject(device);
            Marshal.ReleaseComObject(manager);
            Marshal.ReleaseComObject(sessions);
            return(volumeObj);
        }