Removed test code for disposal
This commit is contained in:
parent
28087b7730
commit
145dcb6148
|
@ -1,199 +1,176 @@
|
||||||
using EonaCat.VolumeMixer.Managers;
|
using EonaCat.VolumeMixer.Managers;
|
||||||
using EonaCat.VolumeMixer.Models;
|
using EonaCat.VolumeMixer.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
// 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.
|
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
||||||
[STAThread]
|
[STAThread]
|
||||||
static async Task Main()
|
static async Task Main()
|
||||||
{
|
{
|
||||||
// Playback management example
|
// Playback management example
|
||||||
using (var volumeMixer = new VolumeMixerManager())
|
using (var volumeMixer = new VolumeMixerManager())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
// Get all audio PLAYBACK devices
|
||||||
|
var devices = await volumeMixer.GetAudioDevicesAsync(DataFlow.Output);
|
||||||
|
Console.WriteLine($"Found {devices.Count} playback devices:");
|
||||||
|
|
||||||
|
foreach (var device in devices)
|
||||||
{
|
{
|
||||||
var input = await volumeMixer.GetAudioDevicesAsync(DataFlow.Input);
|
Console.WriteLine($"- {device.Name} (Default: {device.IsDefault})");
|
||||||
var output = await volumeMixer.GetAudioDevicesAsync(DataFlow.Output);
|
Console.WriteLine($" Volume: {await device.GetMasterVolumeAsync():P0}");
|
||||||
var input2 = await volumeMixer.GetMicrophonesAsync();
|
Console.WriteLine($" Muted: {await device.GetMasterMuteAsync()}");
|
||||||
|
device.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var item in input)
|
// Default playback device
|
||||||
|
using (var defaultDevice = await volumeMixer.GetDefaultAudioDeviceAsync(DataFlow.Output))
|
||||||
|
{
|
||||||
|
if (defaultDevice != null)
|
||||||
{
|
{
|
||||||
item.Dispose();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var item in input2)
|
// Example Set volume of default device
|
||||||
|
if (defaultDevice != null)
|
||||||
{
|
{
|
||||||
item.Dispose();
|
// 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}");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var item in output)
|
if (success)
|
||||||
{
|
{
|
||||||
item.Dispose();
|
Console.WriteLine("Volume increased by 95% successfully.");
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Failed to increase volume.");
|
||||||
// Get all audio PLAYBACK devices
|
}
|
||||||
var devices = await volumeMixer.GetAudioDevicesAsync(DataFlow.Output);
|
}
|
||||||
Console.WriteLine($"Found {devices.Count} playback devices:");
|
|
||||||
|
Console.WriteLine($"Default mic volume: {await volumeMixer.GetMicrophoneVolumeAsync():P0}");
|
||||||
foreach (var device in devices)
|
Console.WriteLine($"Default mic muted: {await volumeMixer.GetMicrophoneMuteAsync()}");
|
||||||
{
|
|
||||||
Console.WriteLine($"- {device.Name} (Default: {device.IsDefault})");
|
// Set microphone volume to 60%
|
||||||
Console.WriteLine($" Volume: {await device.GetMasterVolumeAsync():P0}");
|
bool result = await volumeMixer.SetMicrophoneVolumeAsync(0.6f);
|
||||||
Console.WriteLine($" Muted: {await device.GetMasterMuteAsync()}");
|
Console.WriteLine($"Set microphone volume to 60%: {result}");
|
||||||
device.Dispose();
|
|
||||||
}
|
// Set specific microphone by name
|
||||||
|
result = await volumeMixer.SetMicrophoneVolumeByNameAsync("USB", 0.7f);
|
||||||
// Default playback device
|
Console.WriteLine($"Set USB microphone volume to 70%: {result}");
|
||||||
using (var defaultDevice = await volumeMixer.GetDefaultAudioDeviceAsync(DataFlow.Output))
|
|
||||||
{
|
}
|
||||||
if (defaultDevice != null)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"\nDefault playback device: {defaultDevice.Name}");
|
Console.WriteLine($"Error: {exception.Message}");
|
||||||
|
}
|
||||||
// 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}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue