// Author: Simon Gockner // Created: 2021-04-10 // Copyright(c) 2021 SimonG. All Rights Reserved. using System.Runtime.InteropServices; using Lib.Audio.Interfaces; using NAudio.CoreAudioApi; namespace Lib.Audio { public class MasterControllable : IMasterControllable { private readonly AudioEndpointVolume _audioEndpointVolume; private readonly string _name; public MasterControllable(AudioEndpointVolume audioEndpointVolume, string name) { _audioEndpointVolume = audioEndpointVolume; _name = name; } public void SetVolume(float volume) { try { _audioEndpointVolume.MasterVolumeLevelScalar = volume; } catch (InvalidComObjectException) { // MasterControllable? masterControllable = _controllableCollector.Controllables.OfType().FirstOrDefault(c => c.Equals(this)); // if (masterControllable != null) // masterControllable._audioEndpointVolume.MasterVolumeLevelScalar = volume; } catch (COMException) { } } public void Mute() => _audioEndpointVolume.Mute = true; public void UnMute() => _audioEndpointVolume.Mute = false; public override string ToString() => _name; } }