3

In Windows 7 there are multiple playback devices.

Example (on my laptop): Speakers and Dual Headphones Independent Dual Headphones SPDIF (Digital Out via HP Dock)

The situation is thus: I am writing an app that lets the user choose the output device and save this into the settings of the app. So it offers the user a choice of all Directsound devices in a combobox. The user selects the one he prefers and saves it.

My requirement is: On initial load of this list, I want to select the default device (as set in Windows 7 - Control Panel -> Sound -> Playback tab)

So my code to enumerate the audio output devices is:

Code:

procedure TForm1.FillDevices;
var
  AudioDevEnum: TSysDevEnum;
  n: string;
  i, ps: integer;
begin

  AudioDevEnum := TSysDevEnum.Create(CLSID_AudioRendererCategory);
  try

    if AudioDevEnum.CountFilters = 0 then
      Exit;

    for i := 0 to AudioDevEnum.CountFilters - 1 do
    begin
      n  := AudioDevEnum.Filters[i].FriendlyName;
      ps := pos('DirectSound: ', n);
      if ps <> 0 then
      begin
        ps := pos('Modem', n);
        if ps = 0 then
        begin
          // Delete(n, 1, 13);
          lstDevices.Items.Add(n);
        end;
      end;
    end;
    lstDevices.ItemIndex := 0;

  finally
    AudioDevEnum.Free;
  end;
end;

After getting the list, I want to detect the item which is set as the 'default device' in the sound control panel, and select it. This is so that application saves the correct device the first time without needing the user to do this job.

Can this be done? How?

Thanks in advance.

EDIT: Note that I want to select and save (to INI file) the default device so that it can be used by my application to output sound (via the DSPack component). I do not want to change the Windows setting.

  • lookup registry settings, sorry I can't be more helpful. –  Aug 23 '11 at 06:28
  • Is this the absolutely only way to do it? Which are the keys then - for Vista/7 and for XP ? –  Aug 23 '11 at 07:02
  • I'm pretty sure that's the only way since Windows is storing it's settings within registry, which are those? that's for you to find out/research... –  Aug 23 '11 at 10:07
  • so you're saying its not possible with the DSPack components? –  Aug 23 '11 at 12:06
  • save and restore a device index. If the user add a device or change the default windows device, he's responsible for updating application settings. Users are not stupid they can handle this. – az01 Aug 24 '11 at 17:54
  • yes, my intention is to do this exactly. but if you read my question you will see that i am specifically asking about how to get the currently set device - so that i can set the index in the first place ? –  Aug 25 '11 at 04:46

1 Answers1

2

Here's a method that queries the driver for preferred playback device(http://msdn.microsoft.com/en-us/library/aa909815.aspx), GetWaveOutDeviceList will return the list of devices, GetWaveOutDevice will return the index in the list of the prefered device.

// this method will return the index in the list
function GetWaveOutDevice: Cardinal;
const
  DRVM_MAPPER=$2000;
  DRVM_MAPPER_PREFERRED_GET = DRVM_MAPPER + 21;
  DRVM_MAPPER_PREFERRED_SET = DRVM_MAPPER + 22;
var
 LDW2: Cardinal;
begin
 Result := $FFFFFFFF;
 LDW2 := 0;
 waveOutMessage( WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, DWORD( @Result ), DWORD( @LDW2 ) );
end;

// this method will retrieve the list of devices
procedure GetWaveOutDeviceList(List: TStrings);
var
 Index: Integer;
 LCaps: WAVEOUTCAPS;
begin
  List.Clear;
  for Index := 0 to waveOutGetNumDevs -1 do begin
    waveOutGetDevCaps( Index, @LCaps, SizeOf( LCaps ) );
    List.add( LCaps.szPname );
  end;
end;

If you would like to get the recording devices, just replace "WaveOut" with "WaveIn" in the above to methods.

  • I am hoping that the DSPack list of Directsound devices maps exactly to this list so that the user-selected index will be the same. –  Aug 26 '11 at 06:30
  • well, there is a way in DirectX as well, however I haven't played too much with it, sorry... The WaveOut is also a good alternative because it's supported from WinXP(the earliest I've tested) up until W7 and most likely W8. –  Aug 26 '11 at 21:35
  • Works on Windows 10 too – Xel Naga Jun 14 '20 at 13:24