示例#1
0
        public static bool?GetApplicationMute(int pid)
        {
            ISimpleAudioVolume volume = GetVolumeObject(pid);

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

            bool mute;

            try
            {
                volume.GetMute(out mute);
            }
            finally
            {
                if (volume != null)
                {
                    Marshal.ReleaseComObject(volume);
                }
            }

            return(mute);
        }
示例#2
0
        /// <summary>
        /// 软件静音
        /// </summary>
        /// <param name="user">是否为用户操作</param>
        public static void Mute(bool user = false)
        {
            try
            {
                if (!_isInited)
                {
                    InitSoundAPI();
                }

                if (user)
                {
                    userMute = !userMute;
                }

                bool newValue = !isMute;
                simpleAudioVolume.SetMute(newValue, Guid.NewGuid());

                bool resultValue;
                simpleAudioVolume.GetMute(out resultValue);

                isMute = resultValue;
            }
            catch
            {
                isMute = false;
            }
        }
示例#3
0
        public bool GetMute()
        {
            bool bValue;

            _SimpleAudioVolume.GetMute(out bValue);

            return(bValue);
        }
         public static bool? GetApplicationMute(string name)
         {
             ISimpleAudioVolume volume = GetVolumeObject(name);
             if (volume == null)
                 return null;
 
             bool mute;
             volume.GetMute(out mute);
             return mute;
         }
示例#5
0
            // Get the mute state
            public static bool?GetApplicationMute(ISimpleAudioVolume volume)
            {
                if (volume == null)
                {
                    return(null);
                }

                bool mute;

                volume.GetMute(out mute);
                return(mute);
            }
        public static bool?GetApplicationMute(int pid)
        {
            ISimpleAudioVolume volume = GetVolumeObject(pid);

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

            bool mute;

            volume.GetMute(out mute);
            return(mute);
        }
示例#7
0
        public static bool?GetApplicationMute(string target_display_name)
        {
            ISimpleAudioVolume volume = GetVolumeObject(target_display_name);

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

            bool mute;

            volume.GetMute(out mute);
            return(mute);
        }
示例#8
0
        // gets mute status of process
        public static bool?GetApplicationMute(uint processId)
        {
            ISimpleAudioVolume volume = GetVolumeObject(processId);

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

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
示例#9
0
        public static bool?GetApplicationMute(int pid)
        {
            ISimpleAudioVolume volume = VolumeMixer.GetCoreAudioObject <ISimpleAudioVolume>(pid);

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

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
示例#10
0
        internal static bool IsSpotifyMuted()
        {
            ISimpleAudioVolume volume = GetSpotifyVolumeObject();

            if (volume == null)
            {
                throw new COMException("Volume object creation failed");
            }

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
示例#11
0
        /// <summary>
        /// ミュート状態を取得します。
        /// </summary>
        /// <param name="processID">対象のプロセスID。</param>
        /// <returns>ミュートされていれば true。</returns>
        public static bool GetApplicationMute(uint processID)
        {
            ISimpleAudioVolume volume = GetVolumeObject(processID);

            if (volume == null)
            {
                throw new ArgumentException(ErrorMessageNotFound);
            }

            bool mute;

            volume.GetMute(out mute);

            Marshal.ReleaseComObject(volume);
            return(mute);
        }
示例#12
0
        public static Volume GetInstance()
        {
            var volume = new Volume();

            var deviceEnumeratorType = Type.GetTypeFromCLSID(new Guid(ComCLSIDs.MMDeviceEnumeratorCLSID));
            var devenum = (IMMDeviceEnumerator)Activator.CreateInstance(deviceEnumeratorType);

            IMMDevice device;

            devenum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out device).ThrowIfError();

            object objSessionManager;

            device.Activate(new Guid(ComIIDs.IAudioSessionManager2IID), (uint)CLSCTX.CLSCTX_INPROC_SERVER, IntPtr.Zero, out objSessionManager).ThrowIfError();
            var sessionManager = objSessionManager as IAudioSessionManager2;

            if (sessionManager == null)
            {
                throw new Exception("Session is not found.");
            }

            IAudioSessionEnumerator sessions;

            sessionManager.GetSessionEnumerator(out sessions).ThrowIfError();

            // sessionID は空にするとデフォルトセッションが取れるらしい
            ISimpleAudioVolume simpleAudioVolume;

            sessionManager.GetSimpleAudioVolume(Guid.Empty, 0, out simpleAudioVolume).ThrowIfError();
            volume.simpleAudioVolume = simpleAudioVolume;

            simpleAudioVolume.GetMute(out volume._IsMute).ThrowIfError();

            // sessionControl のインスタンスは取っておかないと通知来なくなる
            sessionManager.GetAudioSessionControl(Guid.Empty, 0, out volume.sessionControl).ThrowIfError();
            volume.sessionControl.RegisterAudioSessionNotification(volume).ThrowIfError();

            return(volume);
        }
示例#13
0
        internal static bool IsSpotifyMuted()
        {
            Process[] p = Process.GetProcessesByName(SpotifyProcessName);
            if (p.Length == 0)
            {
                throw new Exception("Spotify process is not running or was not found!");
            }

            int pid = p[0].Id;

            ISimpleAudioVolume volume = GetVolumeObject(pid);

            if (volume == null)
            {
                throw new COMException("Volume object creation failed");
            }

            bool mute;

            volume.GetMute(out mute);
            Marshal.ReleaseComObject(volume);
            return(mute);
        }
示例#14
0
        /// <summary>
        /// Application entry point
        /// </summary>
        /// <param name="args">Command line arguments</param>
        public static void Main(string[] args)
        {
            try
            {
                // If we were called from a command line then attache to the parent console.
                // We are running as a Windows Application instead of a Console application but
                // we still want to be able to write to the console.
                AttachConsole(-1);

                if (args != null && args.Length >= 1 && args[0] != null && !args[0].Equals("-?"))
                {
                    // First command line argument must contain the process name or -?
                    Process            selectedProcess = null;
                    ISimpleAudioVolume volumeControl   = null;

                    FindMatchingProcess(args[0], out selectedProcess, out volumeControl);

                    if (selectedProcess != null)
                    {
                        Console.WriteLine("Selected Process: " + selectedProcess.ProcessName);

                        // Loop through the rest of the parameters and send the message
                        for (int argIndex = 1; argIndex < args.Length; argIndex++)
                        {
                            switch (args[argIndex])
                            {
                            case "-pp":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PLAY_PAUSE);
                                Console.WriteLine("Message Sent: Play/Pause");
                                break;
                            }

                            case "-p":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PLAY);
                                Console.WriteLine("Message Sent: Play");
                                break;
                            }

                            case "-pa":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PAUSE);
                                Console.WriteLine("Message Sent: Pause");
                                break;
                            }

                            case "-s":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_STOP);
                                Console.WriteLine("Message Sent: Stop");
                                break;
                            }

                            case "-vm":
                            {
                                if (volumeControl != null)
                                {
                                    // Send using the Core Audio API
                                    bool muted;

                                    // Get the current state
                                    volumeControl.GetMute(out muted);

                                    // Toggle the state
                                    volumeControl.SetMute(!muted, Guid.Empty);
                                }
                                else
                                {
                                    SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_MUTE);
                                }

                                Console.WriteLine("Message Sent: Volume Mute");
                                break;
                            }

                            case "-vu":
                            {
                                if (volumeControl != null)
                                {
                                    float currentVolume;

                                    // Get the Current Volume Level
                                    volumeControl.GetMasterVolume(out currentVolume);

                                    // Increment the volume
                                    if (currentVolume + VolumeIncrement >= MaxVolume)
                                    {
                                        volumeControl.SetMasterVolume(MaxVolume, Guid.Empty);
                                    }
                                    else
                                    {
                                        volumeControl.SetMasterVolume(currentVolume + VolumeIncrement, Guid.Empty);
                                    }
                                }
                                else
                                {
                                    SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP);
                                }

                                Console.WriteLine("Message Sent: Volume Up");
                                break;
                            }

                            case "-vd":
                            {
                                if (volumeControl != null)
                                {
                                    float currentVolume;

                                    // Get the current volume level
                                    volumeControl.GetMasterVolume(out currentVolume);

                                    // Decrement the volume
                                    if (currentVolume - VolumeIncrement <= MinVolume)
                                    {
                                        volumeControl.SetMasterVolume(MinVolume, Guid.Empty);
                                    }
                                    else
                                    {
                                        volumeControl.SetMasterVolume(currentVolume - VolumeIncrement, Guid.Empty);
                                    }
                                }
                                else
                                {
                                    SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_DOWN);
                                }

                                Console.WriteLine("Message Sent: Volume Down");
                                break;
                            }

                            case "-mvm":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_MUTE);
                                Console.WriteLine("Message Sent: Master Volume Mute");
                                break;
                            }

                            case "-mvu":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_UP);
                                Console.WriteLine("Message Sent: Master Volume Up");
                                break;
                            }

                            case "-mvd":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_VOLUME_DOWN);
                                Console.WriteLine("Message Sent: Master Volume Down");
                                break;
                            }

                            case "-nt":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_NEXTTRACK);
                                Console.WriteLine("Message Sent: Next Track");
                                break;
                            }

                            case "-pt":
                            {
                                SendMessage(selectedProcess.MainWindowHandle, WM_APPCOMMAND, 0, APPCOMMAND_MEDIA_PREVIOUSTRACK);
                                Console.WriteLine("Message Sent: Previous Track");
                                break;
                            }

                            case "-?":
                            {
                                DisplayUsage();
                                break;
                            }

                            default:
                            {
                                Console.WriteLine("Ignoring unrecognized command: " + args[argIndex]);
                                break;
                            }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No process with the name " + args[0] + " is currently running.");
                    }

                    if (volumeControl != null)
                    {
                        Marshal.ReleaseComObject(volumeControl);
                    }
                }
                else
                {
                    DisplayUsage();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred:\r\n" + e.ToString());
            }
        }
        public static bool?GetApplicationMute(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;
                    bool mute;
                    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;
                                volumeControl.GetMute(out mute);
                                return(mute);
                            }
                        } finally {
                            if (ctl != null)
                            {
                                Marshal.ReleaseComObject(ctl);
                            }
                        }
                    }
                    return(null);
                } finally {
                    if (speakers != null)
                    {
                        Marshal.ReleaseComObject(speakers);
                    }
                    if (mgr != null)
                    {
                        Marshal.ReleaseComObject(mgr);
                    }
                    if (sessionEnumerator != null)
                    {
                        Marshal.ReleaseComObject(sessionEnumerator);
                    }
                    if (deviceEnumerator != null)
                    {
                        Marshal.ReleaseComObject(deviceEnumerator);
                    }
                }
            }
        }
示例#16
0
 public bool GetApplicationMute(ISimpleAudioVolume volume)
 {
     bool mute = false;
     volume.GetMute(out mute);
     return mute;
 }