Skip to content

Manage audio sessions with windows service #75

@ValentinJT

Description

@ValentinJT

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;
                    }
                }
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions