GetSession() 개인적인 메소드

private GetSession ( int SessionCount, IAudioSessionControl** Session ) : HRESULT,
SessionCount int
Session IAudioSessionControl**
리턴 HRESULT,
예제 #1
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);
                }
            }
        }
예제 #2
0
        private 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);
                }
            }
        }
예제 #3
0
        public static List <Process> GetAudioProcesses()
        {
            IMMDeviceEnumerator     deviceEnumerator  = null;
            IAudioSessionEnumerator sessionEnumerator = null;
            IAudioSessionManager2   mgr   = null;
            IMMDevice      speakers       = null;
            List <Process> audioProcesses = new List <Process>();

            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
                for (int i = 0; i < count; ++i)
                {
                    IAudioSessionControl2 ctl = null;
                    try
                    {
                        sessionEnumerator.GetSession(i, out ctl);
                        ctl.GetProcessId(out int cpid);

                        audioProcesses.Add(Process.GetProcessById(cpid));
                    }
                    finally
                    {
                        if (ctl != null)
                        {
                            Marshal.ReleaseComObject(ctl);
                        }
                    }
                }

                return(audioProcesses);
            }
            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);
                }
            }
        }
예제 #4
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);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// 判断是否存在进程
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        public static bool IsExistProcessName(string processName)
        {
            try
            {
                //标识
                bool flag = false;
                // get the speakers (1st render + multimedia) device
                IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                IMMDevice           speakers         = null;
                if (deviceEnumerator != 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 = null;

                if (speakers != null)
                {
                    speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
                }

                IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
                // enumerate sessions for on this device
                IAudioSessionEnumerator sessionEnumerator = null;
                if (mgr != null)
                {
                    mgr.GetSessionEnumerator(out sessionEnumerator);
                }

                int count = 0;

                if (sessionEnumerator != null)
                {
                    sessionEnumerator.GetCount(out count);
                }


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

                    sessionEnumerator.GetSession(i, out ctl);
                    ctl2 = ctl as IAudioSessionControl2;

                    uint pid = 0;

                    string sout1 = "";
                    string sout2 = "";
                    int    a     = 0;


                    if (ctl2 != null)
                    {
                        ctl2.GetSessionIdentifier(out sout1);
                        ctl2.GetProcessId(out pid);
                        ctl2.GetSessionInstanceIdentifier(out sout2);
                    }


                    if (sout2.ToUpper().Contains(processName.ToUpper()))
                    {
                        ISimpleAudioVolume volumeControl = null;
                        volumeControl = ctl as ISimpleAudioVolume;
                        Guid guid = Guid.Empty;

                        AudioSessionState state;
                        ctl2.GetState(out state);
                        if (state.ToString() == "AudioSessionStateActive")
                        {
                            //volumeControl.GetMasterVolume(out Ad_Volume_level);
                            flag = true;
                        }
                    }

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

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

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

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

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

                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
                return(flag);
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLogToFile(string.Format("日志定位Controller.cs-IsExistProcessName(string processName)判断是否存在进程错误-错误信息:{0} {1}", ex.Message.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), true);
                throw ex;
            }
        }
예제 #6
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);
                }
            }
        }
예제 #7
0
        public static VolumeControl GetVolumeControl(HashSet <int> processIds)
        {
            if (processIds == null)
            {
                return(null);
            }

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

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

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

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

                // Get volume control
                for (var 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 (processIds.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);
        }
예제 #8
0
 public AudioSession this[int index] {
     get {
         sessionEnumerator.GetSession(index, out var session);
         return(new AudioSession(session));
     }
 }
        public static void SetApplicationMute(string procname, bool mute)
        {
            IAudioSessionManager2   mgr = null;
            IAudioSessionEnumerator sessionEnumerator = null;

            try
            {
                ISimpleAudioVolume obj = null;
                mgr = GetAudioSessionManager();
                if (mgr == null)
                {
                    return;
                }

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

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

                    IAudioSessionControl2 ctl2 = ctl as IAudioSessionControl2;
                    if (ctl2 != null)
                    {
                        try
                        {
                            if (new AudioSession(ctl2).Process.ProcessName.ToLower().Equals(procname))
                            {
                                obj = ctl2 as ISimpleAudioVolume;
                                if (mute)
                                {
                                    //obj.SetMute(mute, Guid.Empty);
                                    try
                                    {
                                        obj.GetMasterVolume(out volume); // Save old volume
                                    }
                                    catch (Exception e)
                                    {
                                    }
                                    obj.SetMasterVolume(0f, Guid.Empty);
                                }
                                else
                                {
                                    obj.SetMasterVolume(volume, Guid.Empty);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }
                Marshal.ReleaseComObject(sessionEnumerator);
                Marshal.ReleaseComObject(mgr);
            }
            catch (Exception spotify_hire_me)
            {
                Console.WriteLine(spotify_hire_me);
            }
            finally
            {
                Marshal.ReleaseComObject(sessionEnumerator);
                Marshal.ReleaseComObject(mgr);
            }
            return;
        }