PDA

View Full Version : help



Aeronautics
18-05-2003, 12:25 PM
I've tried to get directsound working, but so far I haven't heard any sound from my computer. I'm using directsound.pas (so no components, like TDXSound or whatever).

Could anyone write an example code how to play sound with Directsound?

Ultra
18-05-2003, 11:00 PM
It's nothing fancy and I haven't bothered checking for errors but it works! :)


unit Unit1;

interface

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

type
TForm1 = class(TForm)
btnPlay: TButton;
btnStop: TButton;
btnLoad: TButton;
OpenDialog1: TOpenDialog;
procedure btnPlayClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
Loader: IDirectMusicLoader8;
Performance: IDirectMusicPerformance8;
Segment: IDirectMusicSegment8;
AudioPath: IDirectMusicAudioPath;
IDMSS_TEMP: IDirectMusicSegmentState;
IDM_TEMP: IDirectMusic;
IDS_TEMP: IDirectSound;
Path: PChar;
FileName: PWideChar;
procedure Init;
procedure DeInit;
procedure Load(PathName: String);
procedure Play;
procedure Stop;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Init;
begin
CoInitialize(nil);
CoCreateInstance(CLSID_DirectMusicLoader, nil, CLSCTX_INPROC, IID_IDirectMusicLoader8, Loader);
CoCreateInstance(CLSID_DirectMusicPerformance, nil, CLSCTX_INPROC, IID_IDirectMusicPerformance8, Performance);
CoCreateInstance(CLSID_DirectSound, nil, CLSCTX_INPROC, IID_IDirectSound, IDS_TEMP);
CoCreateInstance(CLSID_DirectMusic, nil, CLSCTX_INPROC, IID_IDirectMusic, IDM_TEMP);

Performance.InitAudio(nil, nil, 0, DMUS_APATH_DYNAMIC_STEREO, 32, DMUS_AUDIOF_ALL, nil);
Performance.CreateStandardAudioPath(DMUS_APATH_DYN AMIC_3D, 32, True, AudioPath);

GetMem(Path, MAX_PATH);
GetMem(FileName, MAX_PATH);
end;

procedure TForm1.DeInit;
begin
Performance.Stop(Segment, IDMSS_TEMP, 0, 0);

Loader := nil;
Performance := nil;
Segment := nil;
IDS_TEMP := nil;
IDM_TEMP := nil;
IDMSS_TEMP := nil;

CoUninitialize;
end;

procedure TForm1.Load(PathName: String);
begin
Path := PChar(PathName);
MultiByteToWideChar(CP_ACP, 0, Path, -1, FileName, MAX_PATH);

Loader.LoadObjectFromFile(CLSID_DirectMusicSegment , IID_IDirectMusicSegment, FileName, Segment);
Segment.Download(Performance);
end;

procedure TForm1.Play;
begin
CoCreateInstance(CLSID_DirectMusicSegmentState, nil, CLSCTX_INPROC, IID_IDirectMusicSegmentState, IDMSS_TEMP);
Performance.PlaySegmentEx(Segment, nil, nil, 64, 0, IDMSS_TEMP, nil, AudioPath);
IDMSS_TEMP := nil;
end;

procedure TForm1.Stop;
begin
Performance.Stop(Segment, IDMSS_TEMP, 0, 0);
end;

procedure TForm1.btnPlayClick(Sender: TObject);
begin
Play;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
Stop;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
if openDialog1.Execute then
begin
Load(openDialog1.FileName);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Init;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DeInit;
end;

end.

Aeronautics
19-05-2003, 07:56 AM
That will do

Thanks!