Files
desktop/WirelessTextSyncer.Windows/Services/AudioControlService.cs
Misaka 30da1680f2 Add system mute control via setMute action
Introduce a setMute protocol action and an NAudio-backed
AudioControlService so the Android client can toggle the Windows
render endpoint mute state over WebSocket.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-17 22:12:13 +08:00

53 lines
1.3 KiB
C#

using System.Runtime.InteropServices;
using NAudio.CoreAudioApi;
namespace WirelessTextSyncer.Windows.Services;
public sealed class AudioControlService : IAudioControlService, IDisposable
{
private readonly MMDeviceEnumerator enumerator = new();
private bool disposed;
public bool IsMuted
{
get
{
try
{
using var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
return device.AudioEndpointVolume.Mute;
}
catch (COMException exception)
{
AppLogger.Error("Failed to read audio mute state.", exception);
return false;
}
}
}
public void SetMute(bool muted)
{
try
{
using var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
device.AudioEndpointVolume.Mute = muted;
AppLogger.Info($"Audio mute set to {muted}.");
}
catch (COMException exception)
{
AppLogger.Error($"Failed to set audio mute to {muted}.", exception);
}
}
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
enumerator.Dispose();
}
}