Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > Apps

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 10-25-2010, 11:02 PM
Bashk Bashk is offline
Senior Member
 
Join Date: Nov 2001
Posts: 349
Default Play sound in separate thread

Hello everibody

I have to play some wav in a separate thread. So far I could only get Error Access in module ...
The wave file is loaded from RES as in media.RES there is the line:
ring WAVE "ringback.wav"

The Delphi7 source code:

uses MMSystem;
[...]

var ring: Pointer;

type
TSoundPlayerThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;

var playRing: TSoundPlayerThread;

[...]

implementation

{$R media.RES}

procedure TSoundPlayerThread.Execute;
begin
sndPlaySound(ring, SND_MEMORY or SND_NODEFAULT or SND_SYNC or SND_LOOP);
if Terminated then Exit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
playRing.Execute;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
playRing.DoTerminate;
end;

initialization
// Here we use some castings to avoid using another variable
ring := Pointer(FindResource(hInstance, 'ring', 'wave'));
if ring <> nil then begin
ring := Pointer(LoadResource(hInstance, HRSRC(ring)));
if ring <> nil then ring := LockResource(HGLOBAL(ring));
end;
end.


Any hints on this issue?
Any idea on how to execute a thread and stop it and reexecute at button's click?

Thank you in advance.
Reply With Quote
  #2  
Old 11-10-2011, 07:47 PM
major major is offline
Senior Member
 
Join Date: Jun 2011
Posts: 150
Default

try something like this:

Code:
type
  TSoundPlayerThread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
public
   constructor Create(Suspended: Boolean);//add thread constructor
  end;

TSoundPlayerThread.Create(Suspended: Boolean);
begin
   inherited;
   FreeOnTerminate := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if not Assigned(TSoundPlayerThread) then
   begin
        playRing := TSoundPlayerThread.Create(False);
       playRing.resume;
   end;
 end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:41 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.