Search code examples
c#interfacenaudioaudiosession

How to convert IAudioSessionControl to AudioSessionControl


I am using NAudio wrapper and I am trying mute the session when it's created.

MMDevice _device = _deviceEnum.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
_device.AudioSessionManager.OnSessionCreated += AudioSessionManager_OnSessionCreated;

private void AudioSessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
{
        //mute session
}

in order to mute the session, I need to convert it from IAudioSessionControl to AudioSessionControl

IAudioSessionControl is an interface and therefore I have no idea how to conver it.

A little help would be appreciated.


Solution

  • Assuming that you mean NAudio.CoreAudioApi.AudioSessionControl there is a constructor for it that takes an IAudioSessionControl as a parameter and encapsulates that interface.

    private void AudioSessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
    {
        AudioSessionControl audioSession = new AudioSessionControl(newSession);
        // mute
        audioSession.SimpleAudioVolume.Mute = true;
    }
    

    The AudioSessionControl object wraps the IAudioSessionControl COM object and exposes additional functionality depending on the other interfaces available. The more direct equivalent of this might be to use the ISimpleAudioVolume interface:

    ISimpleAudioVolume simpleAudioVolume = newSession as ISimpleAudioVolume;
    if (simpleAudioVolume != null)
        simpleAudioVolume.Mute = true;