private static void WriteCurrentAudioProcessesToFile() { // Get all the process Ids Process[] appProcessCollection = Process.GetProcesses(); var appProcessIdCollection = getProcessIds(appProcessCollection).ToHashSet(); // 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); using (System.IO.StreamWriter file = new System.IO.StreamWriter(@".\AudioProcessNames.txt")) { foreach (int pid in commonProcessIdCollection) { Process p = Process.GetProcessById(pid); file.WriteLine(p.ProcessName); } } }
// 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 static void ToggleMute(HashSet <int> appProcessIdCollection) { // 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 currentMute = VolumeMixer.GetApplicationMute(volumeControl); VolumeMixer.SetApplicationMute(volumeControl, !currentMute ?? false); Marshal.ReleaseComObject(volumeControl); } Marshal.ReleaseComObject(defaultAudioDevice); Marshal.ReleaseComObject(sessionManager); Marshal.ReleaseComObject(sessions); }