Exemplo n.º 1
0
        public static float?GetApplicationVolume(int pid)
        {
            ISimpleAudioVolume volume = GetVolumeObject(pid);

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

            float level;

            volume.GetMasterVolume(out level);
            Marshal.ReleaseComObject(volume);
            return(level * 100);
        }
Exemplo n.º 2
0
        internal static float GetSpotifyVolume()
        {
            ISimpleAudioVolume volume = GetSpotifyVolumeObject();

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

            float level;

            volume.GetMasterVolume(out level);
            Marshal.ReleaseComObject(volume);
            return(level * 100);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 音量を取得します。
        /// </summary>
        /// <param name="processID">対象のプロセスID。</param>
        /// <returns>音量( 0.0 - 1.0 )。</returns>
        public static float GetApplicationVolume(uint processID)
        {
            ISimpleAudioVolume volume = GetVolumeObject(processID);

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

            float level;

            volume.GetMasterVolume(out level);

            Marshal.ReleaseComObject(volume);
            return(level);
        }
Exemplo n.º 4
0
        internal static float GetSpotifyVolume()
        {
            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");
            }

            float level;

            volume.GetMasterVolume(out level);
            Marshal.ReleaseComObject(volume);
            return(level * 100);
        }
Exemplo n.º 5
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());
            }
        }
Exemplo n.º 6
0
        public static float?GetApplicationVolume(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;
                    float level;
                    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.GetMasterVolume(out level);
                                return(level * 100);
                            }
                        } 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);
                    }
                }
            }
        }
Exemplo n.º 7
0
 public float GetApplicationVolume(ISimpleAudioVolume volume)
 {
     float level = 0;
     volume.GetMasterVolume(out level);
     return level * 100;
 }
        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);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="type"></param>
        /// <param name="processName"></param>
        /// <param name="volumeLevel"></param>
        public static void SetVolume(string type, string processName, float volumeLevel, ref float adVolumeLevel)
        {
            try
            {
                // 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;
                    IAudioSessionControl2 ctl2;

                    sessionEnumerator.GetSession(i, out ctl);

                    ctl2 = ctl as IAudioSessionControl2;

                    uint pid = 0;

                    string sout1 = "";
                    string sout2 = "";


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

                    if (type == "ad")
                    {
                        if (sout2.ToUpper().Contains(processName.ToUpper()))
                        {
                            ISimpleAudioVolume volumeControl = null;
                            volumeControl = ctl as ISimpleAudioVolume;
                            Guid guid = Guid.Empty;
                            volumeControl.GetMasterVolume(out adVolumeLevel);
                            volumeControl.SetMasterVolume((float)volumeLevel, ref guid);
                        }
                    }
                    else if (type == "voice")
                    {
                        if (sout2.ToUpper().Contains(processName.ToUpper()))
                        {
                            ISimpleAudioVolume volumeControl = null;
                            volumeControl = ctl as ISimpleAudioVolume;
                            Guid guid = Guid.Empty;
                            volumeControl.SetMasterVolume((float)volumeLevel, ref guid);
                        }
                    }


                    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);
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteErrorLogToFile(string.Format("日志定位Controller.cs-SetVolume(string type, string processName, float volumeLevel, ref float adVolumeLevel)判断是否存在进程错误-错误信息:{0} {1}", ex.Message.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), true);
                throw ex;
            }
        }
        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;
        }