177 lines
7.6 KiB
C#
177 lines
7.6 KiB
C#
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}");
|
|
}
|
|
}
|
|
}
|
|
}
|