Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage audio sessions with windows service #75

Open
ValentinJT opened this issue Sep 3, 2024 · 0 comments
Open

Manage audio sessions with windows service #75

ValentinJT opened this issue Sep 3, 2024 · 0 comments

Comments

@ValentinJT
Copy link

ValentinJT commented Sep 3, 2024

I'm developing a Windows service that lets you use an external controller to modify the volume of certain applications.
The general volume modification works, but when I want to retrieve all sessions, there are none of the sessions I want (in SetApplicationVolume function).
For your information, my service runs on the local system account. I've already tried using my local account, but it makes no difference.

Do you have a solution? Or is this a bug?

My code :

namespace test {
    internal class AudioManager
    {
        private CoreAudioController controller;
        
        public AudioManager() {
            this.controller = new CoreAudioController();
        }

        public void SetProcessVolume(string[] processus, int volume)
        {
            if(processus.Length == 0) return;
            foreach(string processName in processus)
            {
                SetApplicationVolume(processName, volume);
            }
        }

        public void SetMasterVolume(int volume)
        {
            var defaultPlaybackDevice = controller.DefaultPlaybackDevice;
            if(defaultPlaybackDevice.Volume == volume) return;
            defaultPlaybackDevice.Volume = volume;
            Logger.Info("Master volume changed to \"" + volume + "%\"");

        }

        public CoreAudioDevice GetDevice(string name)
        {
            return controller.GetPlaybackDevices().FirstOrDefault(a => a.Name == name || a.FullName == name || a.InterfaceName == name);
        }

        public void SetDefaultAudioDevice(Guid deviceGuid)
        {
            var device = controller.GetDevice(deviceGuid);
            controller.SetDefaultDevice(device);
            controller.SetDefaultCommunicationsDevice(device);
        }

        public void SetDefaultAudioDevice(string deviceName)
        {
            var device = GetDevice(deviceName);

            if(device != null)
            {
                SetDefaultAudioDevice(device.Id);
            }
        }

        public void SetApplicationVolume(string processName, int volume)
        {
            if(volume < 0 || volume > 100)
            {
                return;
            }

            var defaultPlaybackDevice = controller.DefaultPlaybackDevice;

            // No session here
            var sessions = defaultPlaybackDevice.SessionController.All();
       
            foreach (var session in sessions)
            {
                using(var process = Process.GetProcessById(session.ProcessId))
                {
                    if(process.ProcessName.Equals(processName, StringComparison.OrdinalIgnoreCase))
                    {
                        session.Volume = volume;
                        break;
                    }
                }
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant