Vallrenn
Vallrenn

Reputation: 1

Having issues with NAudio when trying to use AudioSessionManager2 in C#

When referencing AudioSessionManager2 I'm getting the error:

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'AudioSessionManager2' could not be found (are you missing a using directive or an assembly reference?) focusMute project C:\Users\jacka\source\repos\focusMute project\focusMute project\Form1.cs 14 Active

Here is my current code:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using NAudio.CoreAudioApi;
using NAudio.CoreAudioApi.Interfaces;

namespace focusMute_project
{
    public partial class Form1 : Form
    {
        private readonly MMDeviceEnumerator enumerator;
        private readonly MMDevice defaultDevice;
        private readonly AudioSessionManager2 sessionManager;
        private readonly List<AudioSessionControl> sessions;

        public Form1()
        {
            InitializeComponent();

            enumerator = new MMDeviceEnumerator();
            defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            sessionManager = defaultDevice.AudioSessionManager as AudioSessionManager2;
            sessions = new List<AudioSessionControl>();

            sessionManager.SessionCreated += SessionManager_OnSessionCreated;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Activated += Form1_Activated;
            this.Deactivate += Form1_Deactivate;
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            AdjustVolume(1.0f);
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            AdjustVolume(0.5f);
        }

        private void SessionManager_OnSessionCreated(object sender, IAudioSessionControl newSession)
        {
            sessions.Add(new AudioSessionControl(sessionManager, newSession));
        }

        private void AdjustVolume(float volumeLevel)
        {
            foreach (var session in sessions)
            {
                session.SimpleAudioVolume.MasterVolume = volumeLevel;
            }
        }
    }
}

I have tried uninstalling and reinstalling but currently no success from doing that.

Upvotes: 0

Views: 129

Answers (0)

Related Questions