Author Topic: possible to make device watcher to detect if audio device is added/removed?  (Read 166 times)

Jin

  • Guest
Hi
Is it possible to make audio device watcher to detect if audio device is added or removed without timer?
I already made an watcher to do this with timer.
       
Code: [Select]
        _timer = new DispatcherTimer
        {
            Interval = TimeSpan.FromSeconds(2)
        };
        _timer.Tick += UpdateDeviceList;
        _timer.Start();

        private void UpdateDeviceList(object sender, EventArgs e)
        {
            var currentDeviceList = new List<string>();
            int deviceCount = Bass.BASS_GetDeviceCount();

            for (int i = 0; i < deviceCount; i++)
            {
                var deviceInfo = Bass.BASS_GetDeviceInfo(i);
                currentDeviceList.Add(deviceInfo.name);
            }

            // Check for added or removed devices
            foreach (var device in currentDeviceList)
            {
                if (!_previousDeviceList.Contains(device))
                {
                    Console.WriteLine($"Device added: {device}");
                }
            }

            foreach (var device in _previousDeviceList)
            {
                if (!currentDeviceList.Contains(device))
                {
                    Console.WriteLine($"Device removed: {device}");
                }
            }

            // Update the previous device list
            _previousDeviceList = currentDeviceList;
            // Optionally update the UI
            DeviceList.Items = currentDeviceList;
        }
but this includes huge waste of CPU cycles so I want to find an another way.

Chris

  • Posts: 2221
It is possible to set a Sync on a Channel via BASS_SYNC_DEV_FAIL
https://www.un4seen.com/doc/#bass/BASS_ChannelSetSync.html
the first Synctype

Ian @ un4seen

  • Administrator
  • Posts: 26223
BASS doesn't include device notifications (apart from the failure notifications Chris mentions), but BASSWASAPI does (via BASS_WASAPI_SetNotify), so you could use that just for the notifications (assuming you're using Windows). Some info and code for that can be found here:

   www.un4seen.com/forum/?topic=19759.msg138285#msg138285