EonaCat.VolumeMixer 1.0.5

Prefix Reserved
dotnet add package EonaCat.VolumeMixer --version 1.0.5
                    
NuGet\Install-Package EonaCat.VolumeMixer -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EonaCat.VolumeMixer" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EonaCat.VolumeMixer" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="EonaCat.VolumeMixer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EonaCat.VolumeMixer --version 1.0.5
                    
#r "nuget: EonaCat.VolumeMixer, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EonaCat.VolumeMixer@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EonaCat.VolumeMixer&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=EonaCat.VolumeMixer&version=1.0.5
                    
Install as a Cake Tool

EonaCat.VolumeMixer

The EonaCat.VolumeMixer library provides an interface to interact with individual audio sessions on Windows. It allows you to retrieve and modify the volume and mute state of a session, get session metadata, and access information about the associated process.

This class uses Windows Core Audio APIs via COM interop to manage audio sessions, making it suitable for applications that need fine-grained control over audio streams per process or session.

Features

  • Get and set audio session volume asynchronously
  • Get and set mute state asynchronously
  • Retrieve session metadata such as display name, icon path, process ID, and state
  • Get the name of the process owning the session
  • Thread-safe operations with internal synchronization
  • Proper cleanup of COM resources through implementation of IDisposable

Note

Volume and mute setters are asynchronous and include retry logic for reliability. Volume values must be between 0.0 (silent) and 1.0 (max volume). Getting process name returns "Unknown" if the process cannot be found. Thread-safe for concurrent use. Requires Windows Vista or later with Core Audio APIs.

Dependencies

Must be run with permissions that allow access to audio session interfaces.

Accessing session state:

using EonaCat.VolumeMixer;

IAudioSessionControlExtended sessionControl = /* obtained from enumerator */;
IAudioSessionManager sessionManager = /* obtained from audio device */;
var audioSession = new AudioSession(sessionControl, sessionManager);
string displayName = audioSession.DisplayName;
string iconPath = audioSession.IconPath;
uint processId = audioSession.ProcessId;
AudioSessionState state = audioSession.State;

Obtaining current volume and mute state:

// Get current volume (0.0 to 1.0)
float volume = audioSession.GetVolume();

// Set volume asynchronously
bool success = await audioSession.SetVolumeAsync(0.75f);
if (success)
{
    Console.WriteLine("Volume set successfully");
}
else
{
    Console.WriteLine("Failed to set volume");
}

Mute control:

// Get mute status
bool isMuted = audioSession.GetMute();

// Set mute asynchronously
bool muteSuccess = await audioSession.SetMuteAsync(true);
if (muteSuccess)
{
    Console.WriteLine("Muted successfully");
}
else
{
    Console.WriteLine("Failed to mute");
}

Process Information:

string processName = audioSession.GetProcessName();
Console.WriteLine($"Audio session process: {processName}");

Playback management example:

using EonaCat.VolumeMixer.Managers;
using EonaCat.VolumeMixer.Models;
using System;
using System.Threading.Tasks;

class Program
{
    // This file is part of the EonaCat project(s) which is released under the Apache License.
    // See the LICENSE file or go to https://EonaCat.com/License for full license details.
    [STAThread]
    static async Task Main()
    {
        // Playback management example
        using (var volumeMixer = new VolumeMixerManager())
        {
            try
            {
                // Get all audio PLAYBACK devices
                var devices = await volumeMixer.GetAudioDevicesAsync(DataFlow.Output);
                Console.WriteLine($"Found {devices.Count} playback devices:");

                foreach (var device in devices)
                {
                    Console.WriteLine($"- {device.Name} (Default: {device.IsDefault})");
                    Console.WriteLine($"  Volume: {await device.GetMasterVolumeAsync():P0}");
                    Console.WriteLine($"  Muted: {await device.GetMasterMuteAsync()}");
                    device.Dispose();
                }

                // Default playback device
                using (var defaultDevice = await volumeMixer.GetDefaultAudioDeviceAsync(DataFlow.Output))
                {
                    if (defaultDevice != null)
                    {
                        Console.WriteLine($"\nDefault playback device: {defaultDevice.Name}");

                        // Get all audio sessions
                        var sessions = await defaultDevice.GetAudioSessionsAsync();
                        Console.WriteLine($"Active sessions: {sessions.Count}");

                        foreach (var session in sessions)
                        {
                            Console.WriteLine($"- {session.DisplayName} ({await session.GetProcessNameAsync()})");

                            if ((await session.GetProcessNameAsync()).Equals("msedge", StringComparison.OrdinalIgnoreCase))
                            {
                                Console.WriteLine($" Current Volume: {await session.GetVolumeAsync():P0}");
                                await session.SetVolumeAsync(1f).ConfigureAwait(false);
                                Console.WriteLine($" Set to Volume: {await session.GetVolumeAsync():P0}");
                            }
                            else
                            {
                                Console.WriteLine($"  Volume: {await session.GetVolumeAsync():P0}");
                            }
                            Console.WriteLine($"  Muted: {await session.GetMuteAsync()}");
                            session.Dispose();
                        }
                    }

                    // Example Set volume of default device
                    if (defaultDevice != null)
                    {
                        // Unmute the device if it is muted
                        if (await defaultDevice.GetMasterMuteAsync())
                        {
                            Console.WriteLine("Unmuting default playback device...");
                            await defaultDevice.SetMasterMuteAsync(false);
                        }

                        Console.WriteLine($"\nSetting default playback device volume to 1%");
                        bool success = await defaultDevice.SetMasterVolumeAsync(0.1f);

                        // Log the current volume
                        Console.WriteLine($"Current volume: {await defaultDevice.GetMasterVolumeAsync():P0}");

                        for (int i = 0; i < 50; i++)
                        {
                            if (await defaultDevice.GetMasterVolumeAsync() >= 1f)
                            {
                                break;
                            }

                            // Increment volume by 2% each step
                            success = await defaultDevice.StepUpAsync();
                            Console.WriteLine($"Current step volume: {await defaultDevice.GetMasterVolumeAsync():P0}");
                        }

                        if (success)
                        {
                            Console.WriteLine("Volume increased by 95% successfully.");
                        }
                        else
                        {
                            Console.WriteLine("Failed to increase volume.");
                        }

                        // Toggle mute
                        // bool currentMute = await defaultDevice.GetMasterMuteAsync();
                        // success = await defaultDevice.SetMasterMuteAsync(!currentMute);
                        // Console.WriteLine($"Mute toggled: {success}");
                    }
                    else
                    {
                        Console.WriteLine("No default playback device found");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }

            try
            {
                // Get all microphones
                var microphones = await volumeMixer.GetMicrophonesAsync();
                Console.WriteLine($"Found {microphones.Count} microphones:");

                foreach (var mic in microphones)
                {
                    Console.WriteLine($"- {mic.Name} (Default: {mic.IsDefault})");
                    Console.WriteLine($"  Volume: {await mic.GetMasterVolumeAsync():P0}");
                    Console.WriteLine($"  Muted: {await mic.GetMasterMuteAsync()}");
                    mic.Dispose();
                }

                // Default microphone
                using (var defaultMic = await volumeMixer.GetDefaultMicrophoneAsync())
                {
                    Console.WriteLine($"\nSetting default microphone volume to 1%");
                    bool success = await defaultMic.SetMasterVolumeAsync(0.1f);

                    // Log the current volume
                    Console.WriteLine($"Current volume: {await defaultMic.GetMasterVolumeAsync():P0}");

                    for (int i = 0; i < 50; i++)
                    {
                        if (await defaultMic.GetMasterVolumeAsync() >= 1f)
                        {
                            break;
                        }

                        // Increment volume by 2% each step
                        success = await defaultMic.StepUpAsync();
                        Console.WriteLine($"Current step volume: {await defaultMic.GetMasterVolumeAsync():P0}");
                    }

                    if (success)
                    {
                        Console.WriteLine("Volume increased by 95% successfully.");
                    }
                    else
                    {
                        Console.WriteLine("Failed to increase volume.");
                    }
                }

                Console.WriteLine($"Default mic volume: {await volumeMixer.GetMicrophoneVolumeAsync():P0}");
                Console.WriteLine($"Default mic muted: {await volumeMixer.GetMicrophoneMuteAsync()}");

                // Set microphone volume to 60%
                bool result = await volumeMixer.SetMicrophoneVolumeAsync(0.6f);
                Console.WriteLine($"Set microphone volume to 60%: {result}");

                // Set specific microphone by name
                result = await volumeMixer.SetMicrophoneVolumeByNameAsync("USB", 0.7f);
                Console.WriteLine($"Set USB microphone volume to 70%: {result}");

            }
            catch (Exception exception)
            {
                Console.WriteLine($"Error: {exception.Message}");
            }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 is compatible.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 483 7/23/2025