// 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); }
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 void SetVolume(IntPtr hWnd, float volume) { hWnd = FindWindow("MozillaWindowClass", null); if (hWnd == IntPtr.Zero) { return; } uint pID; GetWindowThreadProcessId(hWnd, out pID); if (pID == 0) { return; } VolumeMixer.SetApplicationVolume((int)pID, volume); }
public AudioBalancer() { _inputHook = Hook.GlobalEvents(); Console.WriteLine("Welcome!\n"); Console.WriteLine("The following applications were found via your default playback device: "); var entries = VolumeMixer.GetPresentableMixerEntries(); for (var i = 0; i < entries.Length; i++) { Console.WriteLine(i + " - " + entries[i].Id); } Console.WriteLine( "\nPlease enter the number of the application you would like to balance against the rest."); int?choice = null; while (choice == null) { var userInput = Console.ReadLine(); try { choice = entries.ToList().IndexOf(entries[int.Parse(userInput)]); } catch (Exception) { Console.WriteLine("Sorry, your input did not match a device. Please try again."); } } Console.Clear(); var selected = entries[choice.Value]; var selectedShortName = selected.Id.Split('\\')[1]; entries = VolumeMixer.GetDefaultMixerEntries(); var currentBalance = new ReactiveProperty <int>(); _inputHook.KeyDown += (sender, args) => { if (args.KeyCode == Keys.F10 && args.Control && currentBalance.Value < 50) { currentBalance.Value++; } if (args.KeyCode == Keys.F11 && args.Control && currentBalance.Value > -50) { currentBalance.Value--; } }; currentBalance.Subscribe(i => { VolumeMixer.SetApplicationVolume(selected.PId, 50 + i); foreach (var entry in entries) { if (entry.PId == selected.PId) { continue; } VolumeMixer.SetApplicationVolume(entry.PId, 50 - i); } Console.Clear(); Console.WriteLine("Balancing!"); Console.WriteLine("\n"); Console.WriteLine("Press ctrl-f10 to move focus towards " + selectedShortName); Console.WriteLine("Press ctrl-f11 to move focus towards the non selected apps"); Console.WriteLine("\n"); Console.WriteLine(selectedShortName + ": " + (50 + i) + "\t" + "Other apps: " + (50 - i)); Console.WriteLine("\n"); Console.WriteLine("Press ctrl-c or just close the window to exit"); }); }
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] + ";"); } }