DSPack - 如何获取声音输出的默认设备?

发布于 2024-12-01 08:42:50 字数 1208 浏览 3 评论 0原文

在 Windows 7 中,有多个播放设备。

示例(在我的笔记本电脑上): 扬声器和双耳机 独立双耳机 SPDIF(通过 HP Dock 进行数字输出)

情况如下: 我正在编写一个应用程序,让用户选择输出设备并将其保存到应用程序的设置中。因此,它为用户提供了组合框中所有 Directsound 设备的选择。用户选择他喜欢的一项并保存。

我的要求是: 初始加载此列表时,我想选择默认设备(如 Windows 7 - 控制面板 -> 声音 -> 播放选项卡中设置),

因此我枚举音频输出设备的代码是:

代码:

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;

获取列表后,我想检测声音控制面板中设置为“默认设备”的项目,然后选择它。这样应用程序就能第一次保存正确的设备,而无需用户执行此工作。

这可以做到吗?如何?

提前致谢。

编辑:请注意,我想选择并保存(到 INI 文件)默认设备,以便我的应用程序可以使用它来输出声音(通过 DSPack 组件)。我不想更改 Windows 设置。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-12-08 08:42:50

这是查询首选播放设备的驱动程序的方法(http://msdn.microsoft .com/en-us/library/aa909815.aspx),GetWaveOutDeviceList将返回设备列表,GetWaveOutDevice将返回首选设备列表中的索引 设备。

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

如果您想获取录音设备,只需将上面方法中的“WaveOut”替换为“WaveIn”即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文