예제 #1
0
        public AudioProcess(AudioDevice audioDevice, IAudioSessionControl2 ctl2)
        {
            // Set device
            AudioDevice = audioDevice;

            Process_AudioSessionControl = ctl2;

            // Set process
            uint processID;

            ctl2.GetProcessId(out processID);
            if (processID != 0)
            {
                try
                {
                    Process = Process.GetProcessById((int)processID);
                }
                catch (Exception e)
                {
                }
            }

            // Set SimpleAudioVolume
            Process_SimpleAudioVolume = ctl2 as ISimpleAudioVolume;

            Update();
        }
예제 #2
0
        // Lists all devices, and for each device all processes that are currently playing sound using that device
        public static List <SoundInfoDevice> getSoundInfo()
        {
            List <SoundInfoDevice> soundInfoDevices = new List <SoundInfoDevice>();

            DeviceEnumerator    enumerator       = new DeviceEnumerator();
            IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)enumerator;
            IMMDeviceCollection deviceCollection = deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, DeviceStatemask.DEVICE_STATE_ACTIVE);
            uint deviceCount = deviceCollection.GetCount();

            for (uint i = 0; i < deviceCount; i++)
            {
                SoundInfoDevice soundInfoDevice = new SoundInfoDevice();
                soundInfoDevices.Add(soundInfoDevice);

                IMMDevice device   = deviceCollection.Item(i);
                string    deviceId = device.GetId();
                soundInfoDevice.ID = deviceId;
                IMMPropertyStore propertyStore         = device.OpenPropertyStore(ProperyStoreMode.STGM_READ);
                PropertyKey      propertyKeyDeviceDesc = new PropertyKey();
                propertyKeyDeviceDesc.fmtid = new Guid(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0);
                propertyKeyDeviceDesc.pid   = 2;
                PropVariant deviceNamePtr = propertyStore.GetValue(ref propertyKeyDeviceDesc);
                string      deviceName    = Marshal.PtrToStringUni(deviceNamePtr.pszVal);
                soundInfoDevice.name = deviceName;

                Guid guidAudioSessionManager2             = new Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F");
                IAudioSessionManager2 audioSessionManager = (IAudioSessionManager2)device.Activate(ref guidAudioSessionManager2, (int)ClsCtx.CLSCTX_ALL, IntPtr.Zero);


                IAudioSessionEnumerator sessionEnumerator = audioSessionManager.GetSessionEnumerator();

                int sessionCount = sessionEnumerator.GetCount();
                for (int j = 0; j < sessionCount; j++)
                {
                    IAudioSessionControl  audioSessionControl  = sessionEnumerator.GetSession(j);
                    IAudioSessionControl2 audioSessionControl2 = (IAudioSessionControl2)audioSessionControl;
                    AudioSessionState     state = audioSessionControl.GetState();
                    if (state == AudioSessionState.AudioSessionStateActive)
                    {
                        SoundInfoSession soundInfoSession = new SoundInfoSession();
                        soundInfoDevice.sessions.Add(soundInfoSession);

                        string displayName = audioSessionControl.GetDisplayName();
                        string iconPath    = audioSessionControl.GetIconPath();
                        int    processId   = audioSessionControl2.GetProcessId();
                        string processName = Process.GetProcessById(processId).MainWindowTitle;

                        soundInfoSession.pid        = processId;
                        soundInfoSession.windowName = processName;
                    }
                }
            }

            return(soundInfoDevices);
        }
예제 #3
0
        private static ISimpleAudioVolume GetVolumeObject(int pid)
        {
            // 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);

            // search for an audio session with the required pid

            ISimpleAudioVolume volumeControl = null;

            for (int i = 0; i < count; i++)
            {
                IAudioSessionControl ctl;


                sessionEnumerator.GetSession(i, out ctl);
                IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2;
                //uint pid;
                //ctl2.GetProcessId(out pid);
                //Process p = Process.GetProcessById((int)pid);
                // ctl2.SetDisplayName(p.ProcessName, Guid.Empty);
                uint npid;
                ctl2.GetProcessId(out npid);
                if (pid == npid)
                {
                    volumeControl = ctl2 as ISimpleAudioVolume;
                    break;
                }
                Marshal.ReleaseComObject(ctl);
                Marshal.ReleaseComObject(ctl2);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
            return(volumeControl);
        }
예제 #4
0
        // gets volume object needed for public methods to operate
        private static ISimpleAudioVolume GetVolumeObject(uint processId)
        {
            MMDeviceEnumerator      deviceEnumerator;
            IMMDeviceEnumerator     iDeviceEnumerator;
            IMMDevice               speakers;
            IAudioSessionManager2   mgr;
            IAudioSessionEnumerator sessionEnumerator;
            int count;

            //GetVolumeControls(out deviceEnumerator, out speakers, out mgr, out sessionEnumerator, out count);
            deviceEnumerator  = new MMDeviceEnumerator();
            iDeviceEnumerator = (IMMDeviceEnumerator)deviceEnumerator;
            iDeviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
            Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;

            speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o);
            mgr = (IAudioSessionManager2)o;
            mgr.GetSessionEnumerator(out sessionEnumerator);
            sessionEnumerator.GetCount(out count);

            ISimpleAudioVolume volumeControl = null;

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

                IAudioSessionControl2 asc2 = GetAudioControl2(asc);

                uint currentProcessId;

                asc2.GetProcessId(out currentProcessId);

                if (currentProcessId == processId)
                {
                    volumeControl = asc as ISimpleAudioVolume;
                    Marshal.ReleaseComObject(asc2);
                    break;
                }
                Marshal.ReleaseComObject(asc2);
                Marshal.ReleaseComObject(asc);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(o);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(iDeviceEnumerator);
            Marshal.ReleaseComObject(deviceEnumerator);
            return(volumeControl);
        }
예제 #5
0
        private static ISimpleAudioVolume GetVolumeObject(int pid)
        {
            // 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);

            uint val;

            ISimpleAudioVolume volumeControl = null;

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

                IAudioSessionControl2 ctl2 = (IAudioSessionControl2)ctl;
                ctl2.GetProcessId(out val);

                if (val == pid)
                {
                    volumeControl = ctl as ISimpleAudioVolume;
                    break;
                }
                Marshal.ReleaseComObject(ctl);
                Marshal.ReleaseComObject(ctl2);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
            return(volumeControl);
        }
예제 #6
0
        public static IEnumerable <int> EnumerateApplicationPIDs()
        {
            // 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;
                sessionEnumerator.GetSession(i, out ctl);
                IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2;
                uint upid;
                ctl2.GetProcessId(out upid);
                int pid = (int)upid;
                yield return(pid);

                Marshal.ReleaseComObject(ctl);
                Marshal.ReleaseComObject(ctl2);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
        }
예제 #7
0
    private static ISimpleAudioVolume GetVolumeObject(int pID)
    {
        List <AudioSession>   list = new List <AudioSession>();
        IAudioSessionManager2 mgr  = GetAudioSessionManager();

        if (mgr == null)
        {
            return(null);
        }

        IAudioSessionEnumerator sessionEnumerator;

        mgr.GetSessionEnumerator(out sessionEnumerator);
        int count;

        sessionEnumerator.GetCount(out count);
        ISimpleAudioVolume volumeObjct = null;
        int cID = 0;

        for (int i = 0; i < count; i++)
        {
            IAudioSessionControl ctl;
            sessionEnumerator.GetSession(i, out ctl);
            if (ctl == null)
            {
                continue;
            }

            IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2;
            ctl2.GetProcessId(out cID);
            if (pID == cID)
            {
                volumeObjct = ctl as ISimpleAudioVolume;
                break;
            }
        }
        Marshal.ReleaseComObject(sessionEnumerator);
        Marshal.ReleaseComObject(mgr);
        return(volumeObjct);
    }
예제 #8
0
        private static ISimpleAudioVolume GetVolumeObject(int pid)
        {
            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   mgr = null;
            IMMDevice speakers          = null;

            try
            {
                // get the speakers (1st render + multimedia) device
                deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                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);
                mgr = (IAudioSessionManager2)o;

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

                // search for an audio session with the required process-id
                ISimpleAudioVolume volumeControl = null;
                for (int i = 0; i < count; ++i)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);

                        // NOTE: we could also use the app name from ctl.GetDisplayName()
                        int cpid;
                        ctl.GetProcessId(out cpid);

                        if (cpid == pid)
                        {
                            volumeControl = ctl as ISimpleAudioVolume;
                            break;
                        }
                    }
                    finally
                    {
                        if (ctl != null)
                        {
                            Marshal.ReleaseComObject(ctl);
                        }
                    }
                }

                return(volumeControl);
            }
            finally
            {
                // bp2008 note: it may be necessary to swap the release of [sessionEnumerator] and [speakers], but that is based on a fork of the class that bloats the code by removing the GetVolumeObject method and copy pasting all that boilerplate into each method, so my faith is extremely limited.
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr);
                }
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }
예제 #9
0
        private static ISimpleAudioVolume GetVolumeObject(int pid)
        {
            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   mgr = null;
            IMMDevice speakers          = null;

            try
            {
                // get the speakers (1st render + multimedia) device
                deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                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);
                mgr = (IAudioSessionManager2)o;

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

                // search for an audio session with the required process-id
                ISimpleAudioVolume volumeControl = null;
                for (int i = 0; i < count; ++i)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);

                        // NOTE: we could also use the app name from ctl.GetDisplayName()
                        int cpid;
                        ctl.GetProcessId(out cpid);

                        if (cpid == pid)
                        {
                            volumeControl = ctl as ISimpleAudioVolume;
                            break;
                        }
                    }
                    finally
                    {
                        //if (ctl != null) Marshal.ReleaseComObject(ctl);
                    }
                }

                return(volumeControl);
            }
            finally
            {
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr);
                }
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }
        public static VolumeControl GetVolumeControl(HashSet <int> p)
        {
            if (p == null)
            {
                return(null);
            }

            ISimpleAudioVolume      volumeControl    = null;
            IMMDeviceEnumerator     deviceEnumerator = null;
            IMMDevice               speakers         = null;
            IAudioSessionManager2   sm = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            int ctlPid = 0;

            try {
                // Get default device
                deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);

                // Get session manager
                Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
                speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out object o);
                sm = (IAudioSessionManager2)o;

                // Get sessions
                sm.GetSessionEnumerator(out sessionEnumerator);
                sessionEnumerator.GetCount(out int count);

                // Get volume control
                for (int i = 0; i < count; i++)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);
                        if (ctl == null)
                        {
                            continue;
                        }

                        // Get and compare process id
                        ctl.GetProcessId(out ctlPid);
                        if (p.Contains(ctlPid))
                        {
                            volumeControl = ctl as ISimpleAudioVolume;
                            break;
                        }
                    }
                    finally
                    {
                        if (volumeControl == null && ctl != null)
                        {
                            Marshal.ReleaseComObject(ctl);                                       // Only release if not target session
                        }
                    }
                }
            }
            finally
            {
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (sm != null)
                {
                    Marshal.ReleaseComObject(sm);
                }
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }

            if (volumeControl != null)
            {
                return(new VolumeControl(ctlPid, volumeControl));
            }
            return(null);
        }
예제 #11
0
        public static IEnumerable <ApplicationVolumeInformation> 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(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;
                sessionEnumerator.GetSession(i, out ctl);
                uint   GetProcessID             = 0;
                String GetName                  = "";
                float  GetVolume                = 0;
                String GetIconPath              = "";
                IAudioSessionControl getsession = null;
                getsession = ctl;
                if (ctl is IAudioSessionControl2)
                {
                    IAudioSessionControl2 ctl2 = ((IAudioSessionControl2)ctl);

                    ctl2.GetProcessId(out GetProcessID);
                    ctl2.GetDisplayName(out GetName);

                    String sIconPath;
                    ctl2.GetIconPath(out sIconPath);
                    ISimpleAudioVolume volcast = (ctl2 as ISimpleAudioVolume);
                    float grabvolume;
                    volcast.GetMasterVolume(out grabvolume);
                    GetVolume = grabvolume;
                    try
                    {
                        Process grabProcess = Process.GetProcessById((int)GetProcessID);
                        if (String.IsNullOrEmpty(GetName))
                        {
                            GetName = grabProcess.ProcessName;
                        }
                    }
                    catch (Exception exx)
                    {
                        GetName = "Name Not Available";
                    }
                }
                ApplicationVolumeInformation avi = new ApplicationVolumeInformation(getsession, GetProcessID, GetVolume, GetName, GetIconPath);

                yield return(avi);

                Marshal.ReleaseComObject(ctl);
            }
            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
        }
예제 #12
0
        //считывает текущее значение уровней звука и обновляет UI
        public void UpdateVisualizer()
        {
            if (speakers == null)
            {
                return;
            }

            IAudioSessionManager2   mgr = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionControl    ctl   = null;
            IAudioSessionControl2   ctl2  = null;
            IAudioMeterInformation  meter = null;

            try
            {
                // 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);
                mgr = (IAudioSessionManager2)o;

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

                float max_val = 0.0f;          //максимальное значение уровня звука для всех сессий
                int   h_min = 50, h_max = 120; //макс. и мин. значение высоты для эллипса

                int   hr;
                uint  pid = 0;
                float val = 0.0f;

                for (int i = 0; i < count; i++)
                {
                    if (ctl != null)
                    {
                        Marshal.ReleaseComObject(ctl); ctl = null;
                    }
                    if (ctl2 != null)
                    {
                        Marshal.ReleaseComObject(ctl2); ctl2 = null;
                    }
                    if (meter != null)
                    {
                        Marshal.ReleaseComObject(meter); meter = null;
                    }

                    //получаем WASAPI-сессию
                    hr = sessionEnumerator.GetSession(i, out ctl);
                    if (hr != 0)
                    {
                        continue;
                    }

                    ctl2 = (IAudioSessionControl2)ctl;
                    pid  = 0;
                    ctl2.GetProcessId(out pid);
                    if (pid != this_pid)
                    {
                        continue;                      //интересуют только сессии текущего процесса
                    }
                    meter = (IAudioMeterInformation)ctl;
                    hr    = meter.GetPeakValue(out val); //получаем уровень звука
                    if (hr != 0)
                    {
                        continue;
                    }
                    if (val > max_val)
                    {
                        max_val = val;
                    }
                }

                //изменяем высоту эллипса в соответствии со значением максимального уровня звука
                ellVisualizer.Height = h_min + max_val * (h_max - h_min);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.GetType().ToString());
            }
            finally
            {
                //очистка ресурсов
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator); sessionEnumerator = null;
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr); mgr = null;
                }

                if (ctl != null)
                {
                    Marshal.ReleaseComObject(ctl); ctl = null;
                }
                if (ctl2 != null)
                {
                    Marshal.ReleaseComObject(ctl2); ctl2 = null;
                }
                if (meter != null)
                {
                    Marshal.ReleaseComObject(meter); meter = null;
                }
            }
        }
예제 #13
0
        private static ISimpleAudioVolume GetVolumeObject()
        {
            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   sessionManager    = null;
            IMMDevice device = null;

            try
            {
                // ReSharper disable once SuspiciousTypeConversion.Global
                deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out device);

                // ReSharper disable once InconsistentNaming
                var    IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
                object obj;
                device.Activate(IID_IAudioSessionManager2, 0, IntPtr.Zero, out obj);
                sessionManager = (IAudioSessionManager2)obj;

                sessionManager.GetSessionEnumerator(out sessionEnumerator);
                int count;
                sessionEnumerator.GetCount(out count);

                ISimpleAudioVolume volumeControl = null;
                for (var i = 0; i < count; i++)
                {
                    IAudioSessionControl  sessionControl  = null;
                    IAudioSessionControl2 sessionControl2 = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out sessionControl);
                        sessionControl2 = sessionControl as IAudioSessionControl2;

                        if (sessionControl2 == null)
                        {
                            // 多分通らない
                            string displayName;
                            sessionControl.GetDisplayName(out displayName);
                            if (displayName == "Norma")
                            {
                                volumeControl = sessionControl as ISimpleAudioVolume;
                                break;
                            }
                        }
                        else
                        {
                            uint processId;
                            sessionControl2.GetProcessId(out processId);
                            if (processId == Process.GetCurrentProcess().Id)
                            {
                                volumeControl = sessionControl2 as ISimpleAudioVolume;
                                break;
                            }
                        }
                    }
                    finally
                    {
                        // うーん

                        /*
                         * if (sessionControl != null)
                         *  Marshal.ReleaseComObject(sessionControl);
                         * if (sessionControl2 != null)
                         *  Marshal.ReleaseComObject(sessionControl2);
                         */
                    }
                }
                return(volumeControl);
            }
            finally
            {
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (sessionManager != null)
                {
                    Marshal.ReleaseComObject(sessionManager);
                }
                if (device != null)
                {
                    Marshal.ReleaseComObject(device);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }
예제 #14
0
        public static List <Application> EnumerateApplications()
        {
            // get the speakers (1st render + multimedia) device

            List <Application> applications = new List <Application>();

            IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            IMMDevice           speakers         = null;

            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)
            {
                IAudioSessionControl2 ctl = null;
                sessionEnumerator.GetSession(i, out ctl);

                string dn;
                string iconPath;
                int    pid;
                ctl.GetProcessId(out pid);
                ctl.GetDisplayName(out dn);
                float volumeLevel   = GetApplicationVolume(dn);
                int   roundedVolume = (int)Math.Round(volumeLevel);
                Console.WriteLine("Application Name: " + dn);

                Console.WriteLine(pid);

                Application application = new Application();

                application.name   = dn;
                application.volume = roundedVolume;
                application.pid    = pid;

                //var proc = Process.GetProcessById(pid);
                //try
                //{
                //    Console.WriteLine(proc.MainModule.FileName);
                //}
                //catch (Exception e)
                //{
                //    Console.WriteLine(e.Message);
                //}


                var query = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process WHERE processid = " + pid.ToString();


                using (var searcher = new ManagementObjectSearcher(query))
                    using (var results = searcher.Get())
                    {
                        var processes = results.Cast <ManagementObject>().Select(x => new
                        {
                            ProcessId      = (UInt32)x["ProcessId"],
                            Name           = (string)x["Name"],
                            ExecutablePath = (string)x["ExecutablePath"]
                        });
                        Console.WriteLine("Applicaiton name from search: " + processes.ElementAt(0).Name);
                        Console.WriteLine("Applicaiton name from search: " + Process.GetProcessById((int)processes.ElementAt(0).ProcessId).MainWindowTitle);
                        if (application.pid == 0)
                        {
                            application.name = "System Sounds";
                        }
                        else
                        {
                            application.name = Process.GetProcessById((int)processes.ElementAt(0).ProcessId).MainWindowTitle;
                        }



                        foreach (var p in processes)
                        {
                            if (System.IO.File.Exists(p.ExecutablePath))
                            {
                                var icon = Icon.ExtractAssociatedIcon(p.ExecutablePath);

                                icon.ToBitmap().Save("icon.bmp");
                                Byte[] bytes       = File.ReadAllBytes("icon.bmp");
                                String encodedIcon = Convert.ToBase64String(bytes);

                                application.icon = encodedIcon;
                                var key = p.ProcessId.ToString();

                                Console.WriteLine(p.ExecutablePath);
                            }
                        }
                    }



                applications.Add(application);

                ctl.GetIconPath(out iconPath);
                Console.WriteLine("IconPath: " + iconPath);

                Marshal.ReleaseComObject(ctl);
            }

            return(applications);

            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
        }
예제 #15
0
        public static List <int> getAllAudioSessions()
        {
            List <int> pipNumbersList = new List <int>();

            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   mgr = null;
            IMMDevice speakers          = null;

            try
            {
                // get the speakers (1st render + multimedia) device
                deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                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);
                mgr = (IAudioSessionManager2)o;

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

                for (int i = 0; i < count; ++i)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);

                        // NOTE: we could also use the app name from ctl.GetDisplayName()
                        int cpid;
                        ctl.GetProcessId(out cpid);
                        pipNumbersList.Add(cpid);
                        //Console.WriteLine(cpid.ToString());
                    }
                    finally
                    {
                        //  if (ctl != null) Marshal.ReleaseComObject(ctl);
                    }
                }
                return(pipNumbersList);
            }
            finally
            {
                if (sessionEnumerator != null)
                {
                    Marshal.ReleaseComObject(sessionEnumerator);
                }
                if (mgr != null)
                {
                    Marshal.ReleaseComObject(mgr);
                }
                if (speakers != null)
                {
                    Marshal.ReleaseComObject(speakers);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
        }