Ejemplo n.º 1
0
        // Change the focused apps volume by the amount specified
        private static void ChangeAppVolume(HashSet <int> appProcessIdCollection, float volumeAmount)
        {
            // Get all the processes that are an audio session
            var defaultAudioDevice       = VolumeMixer.GetOutputDevice();
            var sessionManager           = VolumeMixer.GetAudioSessionManager2(defaultAudioDevice);
            var sessions                 = VolumeMixer.GetAudioSessionEnumerator(sessionManager);
            var audioControls            = VolumeMixer.GetAudioContols(sessions);
            var audioProcessIdCollection = audioControls.Keys.ToHashSet();

            // Get all the processes that are audio sessions of the focused application
            var commonProcessIdCollection = appProcessIdCollection.Intersect(audioProcessIdCollection);

            // Change the volume of all the audio processes of the focused application
            foreach (int processId in commonProcessIdCollection)
            {
                var volumeControl  = audioControls[processId] as ISimpleAudioVolume;
                var newVolumeLevel = VolumeMixer.GetApplicationVolume(volumeControl) + volumeAmount;
                VolumeMixer.SetApplicationVolume(volumeControl, Math.Min(100, Math.Max(0, newVolumeLevel ?? 30f)));
                Marshal.ReleaseComObject(volumeControl);
            }

            Marshal.ReleaseComObject(defaultAudioDevice);
            Marshal.ReleaseComObject(sessionManager);
            Marshal.ReleaseComObject(sessions);
        }
Ejemplo n.º 2
0
        private void Llkl_OnKeyPressed(object sender, LowLevelKeyboardListener.KeyPressedArgs e)
        {
            if (e.KeyPressed == (int)Keys.Add)
            {
                IntPtr handle = GetForegroundWindow();
                uint   pid;
                GetWindowThreadProcessId(handle, out pid);

                var vol = VolumeMixer.GetApplicationVolume((int)pid);
                if (vol.HasValue)
                {
                    VolumeMixer.SetApplicationVolume((int)pid, (float)vol + 2);
                }
            }
            if (e.KeyPressed == (int)Keys.Subtract)
            {
                IntPtr handle = GetForegroundWindow();
                uint   pid;
                GetWindowThreadProcessId(handle, out pid);

                var vol = VolumeMixer.GetApplicationVolume((int)pid);
                if (vol.HasValue)
                {
                    VolumeMixer.SetApplicationVolume((int)pid, (float)vol - 2);
                }
            }
        }
 private async Task MuteCodIntermittently(Process process)
 {
     while (!StopMuting)
     {
         if (VolumeMixer.GetApplicationVolume(process.Id) == 100f)
         {
             if (codLaunchedTime == default)
             {
                 codLaunchedTime = DateTime.Now;
                 DelayedUnmute();
             }
         }
         VolumeMixer.SetApplicationMute(process.Id, true);
         await Task.Delay(50);
     }
     codLaunchedTime = default;
     VolumeMixer.SetApplicationMute(process.Id, false);
 }
Ejemplo n.º 4
0
        private float?GetVolume(IntPtr hWnd)
        {
            hWnd = FindWindow("MozillaWindowClass", null);
            if (hWnd == IntPtr.Zero)
            {
                return(null);
            }

            uint pID;

            GetWindowThreadProcessId(hWnd, out pID);
            if (pID == 0)
            {
                return(null);
            }

            return(VolumeMixer.GetApplicationVolume((int)pID));
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }

            var hWnd = FindWindow("SpotifyMainWindow", "Spotify");

            if (hWnd == IntPtr.Zero)
            {
                hWnd = new IntPtr(int.Parse(args[0]));
            }

            uint pID;

            GetWindowThreadProcessId(hWnd, out pID);
            if (pID == 0)
            {
                return;
            }

            Console.WriteLine("appid:" + hWnd + ";");
            Console.WriteLine("userVolume:" + VolumeMixer.GetApplicationVolume(pID) + ";");

            if (args.Length > 1)
            {
                if (args.Length > 2)
                {
                    if (args[2] == "1")
                    {
                        Console.WriteLine("args[0]:" + args[0] + ";args[1]:" + args[1] + "args[2]:" + args[2] + ";");

                        var curVol = (int)VolumeMixer.GetApplicationVolume(pID);
                        var diff   = curVol - int.Parse(args[1]);
                        for (int i = 1; i <= diff; i++)
                        {
                            VolumeMixer.SetApplicationVolume(pID, curVol - i);
                            System.Threading.Thread.Sleep(10);
                        }
                    }
                    else if (args[2] == "2")
                    {
                        Console.WriteLine("args[0]:" + args[0] + ";args[1]:" + args[1] + "args[2]:" + args[2] + ";");

                        var curVol = (int)VolumeMixer.GetApplicationVolume(pID);
                        var diff   = curVol + int.Parse(args[1]);
                        for (int i = 1; i <= diff; i++)
                        {
                            VolumeMixer.SetApplicationVolume(pID, curVol + i);
                            System.Threading.Thread.Sleep(10);
                        }
                    }
                }
                else
                {
                    VolumeMixer.SetApplicationVolume(pID, float.Parse(args[1]));
                    Console.WriteLine("args[0]:" + args[0] + ";args[1]:" + args[1]);
                }

                Console.WriteLine("appVolume:" + VolumeMixer.GetApplicationVolume(pID) + ";");
            }
            else
            {
                Console.WriteLine("args[0]:" + args[0] + ";");
            }
        }