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>
This commit is contained in:
Misaka
2026-06-17 22:12:13 +08:00
parent ad9a9021cc
commit 30da1680f2
9 changed files with 126 additions and 5 deletions

View File

@@ -0,0 +1,52 @@
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();
}
}

View File

@@ -0,0 +1,8 @@
namespace WirelessTextSyncer.Windows.Services;
public interface IAudioControlService
{
bool IsMuted { get; }
void SetMute(bool muted);
}

View File

@@ -5,11 +5,13 @@ namespace WirelessTextSyncer.Windows.Services;
public sealed class SyncMessageHandler
{
private readonly IKeyboardInjectionService keyboard;
private readonly IAudioControlService audio;
private string remoteText = string.Empty;
public SyncMessageHandler(IKeyboardInjectionService keyboard)
public SyncMessageHandler(IKeyboardInjectionService keyboard, IAudioControlService audio)
{
this.keyboard = keyboard;
this.audio = audio;
}
public void Handle(SyncMessage message)
@@ -38,11 +40,21 @@ public sealed class SyncMessageHandler
break;
case SyncAction.Ping:
break;
case SyncAction.SetMute:
HandleSetMute(message);
break;
default:
throw new InvalidOperationException($"Unsupported sync action: {message.Action}");
}
}
private void HandleSetMute(SyncMessage message)
{
var muted = message.Muted ?? false;
AppLogger.Info($"SetMute received muted={muted}.");
audio.SetMute(muted);
}
private void AppendSubmittedText(string text)
{
if (string.IsNullOrEmpty(text))