sample-service/Models/DevicesUpdater.cs
2025-05-13 21:55:57 +03:00

65 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.ObjectModel;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;
namespace SoundTesting
{
public class
DevicesUpdater : IMMNotificationClient //Кастомный интерфейс уведомлений об изменении количества и состояния подключенных аудиоустройств
{
public ObservableCollection<string> InputDevices { get; set; } = new();
public ObservableCollection<string> OutputDevices { get; set; } = new();
public DevicesUpdater()
{
DeviceListUpdate();
}
private void DeviceListUpdate() //Обновление данных об устройствах
{
InputDevices?.Clear();
OutputDevices?.Clear();
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
var inputs = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
foreach (var device in inputs)
{
InputDevices.Add( device.FriendlyName );
device.Dispose();
}
var outputs = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
foreach (var device in outputs)
{
OutputDevices.Add( device.FriendlyName );
device.Dispose();
}
enumerator.Dispose();
}
public void OnDeviceStateChanged(string deviceId, DeviceState newState) //Изменение состояния устройства
{
}
public void OnDeviceAdded(string deviceId) //Добавление устройства
{
DeviceListUpdate();
}
public void OnDeviceRemoved(string deviceId) //Удаление устройства
{
DeviceListUpdate();
}
public void OnDefaultDeviceChanged(DataFlow flow, Role role, string defaultDeviceId) //Изменение устройства по-умолчанию
{
DeviceListUpdate();
}
public void OnPropertyValueChanged(string deviceId, PropertyKey key) //Изменение свойства устройства
{
}
}
}