• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

在Delphi下使用DirectSound(1):枚举播放设备

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

现在的 Delphi(2010、XE) 已经自带了 DirectX 的相关单元(...\source\rtl\win\).
//枚举函数
function DirectSoundEnumerate(
  lpDSEnumCallback: TDSEnumCallback; //回调函数
  lpContext: Pointer                 //用户指针
): HResult; stdcall; //返回错误代码, 成功则返回 DS_OK(0)

//DirectSoundEnumerate 需要的回调函数的原形:
TDSEnumCallback = function(
  lpGuid: PGUID;            //设备的 GUID
  lpcstrDescription: PChar; //设备描述
  lpcstrModule: PChar;      //模块标识
  lpContext: Pointer        //由 DirectSoundEnumerate 提供的用户指针
): BOOL; stdcall; //返回 True 表示要继续枚举, 不在继续找了就返回 False


这是常见的代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox; //只在窗体上放了一个列表框
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses DirectSound; //!

function EnumCallback(lpGuid: PGUID; lpcstrDescription, lpcstrModule: PChar;
    lpContext: Pointer): BOOL; stdcall;
begin
  Form1.ListBox1.Items.Add(lpcstrDescription);
  Result := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DirectSoundEnumerate(EnumCallback, nil);
end;

end.


在回调函数中直接使用窗体控件不好, 修改如下:
uses DirectSound;

function EnumCallback(lpGuid: PGUID; lpcstrDescription, lpcstrModule: PChar;
    lpContext: Pointer): BOOL; stdcall;
begin
  TStrings(lpContext).Add(lpcstrDescription);
  Result := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DirectSoundEnumerate(EnumCallback, ListBox1.Items);
end;


获取更多信息:
uses DirectSound;

function EnumCallback(lpGuid: PGUID; lpcstrDescription, lpcstrModule: PChar;
    lpContext: Pointer): BOOL; stdcall;
begin
  if lpGuid <> nil then TStrings(lpContext).Add(GUIDToString(lpGuid^));
  TStrings(lpContext).Add(lpcstrDescription);
  if lpcstrModule <> nil then TStrings(lpContext).Add(lpcstrModule);
  TStrings(lpContext).Add(EmptyStr);
  Result := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DirectSoundEnumerate(EnumCallback, ListBox1.Items);
end;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB ~的用法发布时间:2022-07-18
下一篇:
电力电子MATLAB发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap