Forum > Beginners
PlaySound ('dog.wav', 0, snd_Async);
jcaser1948:
I am working through the book "Delpi 7 The Bible
In one of the example programs appears:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TDog.Voice: string;begin Voice := 'Arf Arf'; PlaySound ('dog.wav', 0, snd_Async);end;That of course does not compile since I work in Linux and the books was written for Windows.
Has anyone a suggestion how I could program that in Lazarus for Linux?
Sorry for those silly questions
Thanks
Handoko:
Audio and multimedia libraries available for Pascal:
http://wiki.freepascal.org/Audio_libraries
http://wiki.freepascal.org/Multimedia_Programming
Multiplatform play sound:
http://wiki.freepascal.org/Play_Sound_Multiplatform
wp:
The easiest way to play a soundfile like in "PlaySound" is using the PlaySoundPackage. if you have Laz 1.8 you can find it in the Online Package Manager (Check it, click Install, that's all). Or you go to https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/playsoundpackage/latest_stable/ and download the files uplaysound.pas and aboutplaysound.pas into your project folder; since the latter method does not install the component you must write a few lines of code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses uplaysound; procedure TForm1.Playsound(AWavFile: String; ASync: Boolean);var player: TPlaySound;begin player := TPlaySound.Create(nil); try player.SoundFile := AWavFile; if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync; player.Execute; finally player.Free; end;end;
jcaser1948:
The given procedure works fine,thank you very much.
But I have still trouble with the book2Delphi 7, the Bible where this procedure is used
Here the code from bokk,,adapted
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit abstanimf; {$MODE Delphi} interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Buttons, ExtCtrls, uplaysound; type { TFormAnimals } TFormAnimals = class(TForm) playsound1: Tplaysound; LabelVoice: TLabel; BtnVoice: TButton; RbtnAnimal: TRadioButton; RbtnDog: TRadioButton; RbtnCat: TRadioButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure BtnVoiceClick(Sender: TObject); procedure RbtnAnimalClick(Sender: TObject); procedure RbtnDogClick(Sender: TObject); procedure RbtnCatClick(Sender: TObject); procedure PlaySound(AWavFile: String; ASync: Boolean); private public { Public declarations } end; TAnimal = class public constructor Create; function GetKind: string; function Voice: string; virtual;abstract; private Kind: string; end; TDog = class (TAnimal) public constructor Create; function Voice: string; override; function Eat: string; virtual; end; TCat = class (TAnimal) public constructor Create; function Voice: string;override; function Eat: string; virtual; end; var FormAnimals: TFormAnimals; MyAnimal: TAnimal; implementation {$R *.lfm} procedure TFormAnimals.FormCreate(Sender: TObject);begin MyAnimal := TDog.Create;end; procedure TFormAnimals.FormDestroy(Sender: TObject);begin MyAnimal.Free;end; procedure TFormAnimals.BtnVoiceClick(Sender: TObject);begin LabelVoice.Caption := MyAnimal.Voice;end; constructor TAnimal.Create;begin Kind := 'An animal';end; function TAnimal.GetKind: string;begin GetKind := Kind;end; constructor TDog.Create;begin Kind := 'A dog';end; function TDog.Voice: string;Var VoiceSound:String;begin Voice := 'Arf Arf'; VoiceSound:= 'dog.wav'; FormAnimals.PlaySound(VoiceSound,true); end; function TDog.Eat: string;begin Eat := 'A bone, please!';end; constructor TCat.Create;begin Kind := 'A cat';end; function TCat.Voice: string;Var VoiceSound:String;begin Voice := 'Mieow' ; VoiceSound:= 'cat.wav'; FormAnimals.PlaySound(VoiceSound,true);end; function TCat.Eat: string;begin Eat := 'A mouse, please!';end; procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);begin MyAnimal.Free; MyAnimal := TAnimal.Create;end; procedure TFormAnimals.RbtnDogClick(Sender: TObject);begin MyAnimal.Free; MyAnimal := TDog.Create;end; procedure TFormAnimals.RbtnCatClick(Sender: TObject);begin MyAnimal.Free; MyAnimal := TCat.Create;end; procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean); var player: TPlaySound;begin player := TPlaySound.Create(nil); try player.SoundFile := AWavFile; if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync; player.Execute; finally player.Free; end;end;end. It compile fine but when I get
unit abstanimf;
{$MODE Delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, uplaysound;
type
{ TFormAnimals }
TFormAnimals = class(TForm)
playsound1: Tplaysound;
LabelVoice: TLabel;
BtnVoice: TButton;
RbtnAnimal: TRadioButton;
RbtnDog: TRadioButton;
RbtnCat: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnVoiceClick(Sender: TObject);
procedure RbtnAnimalClick(Sender: TObject);
procedure RbtnDogClick(Sender: TObject);
procedure RbtnCatClick(Sender: TObject);
procedure PlaySound(AWavFile: String; ASync: Boolean);
private
public
{ Public declarations }
end;
TAnimal = class
public
constructor Create;
function GetKind: string;
function Voice: string; virtual;abstract;
private
Kind: string;
end;
TDog = class (TAnimal)
public
constructor Create;
function Voice: string; override;
function Eat: string; virtual;
end;
TCat = class (TAnimal)
public
constructor Create;
function Voice: string;override;
function Eat: string; virtual;
end;
var
FormAnimals: TFormAnimals;
MyAnimal: TAnimal;
implementation
{$R *.lfm}
procedure TFormAnimals.FormCreate(Sender: TObject);
begin
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
MyAnimal.Free;
end;
procedure TFormAnimals.BtnVoiceClick(Sender: TObject);
begin
LabelVoice.Caption := MyAnimal.Voice;
end;
constructor TAnimal.Create;
begin
Kind := 'An animal';
end;
function TAnimal.GetKind: string;
begin
GetKind := Kind;
end;
constructor TDog.Create;
begin
Kind := 'A dog';
end;
function TDog.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Arf Arf';
VoiceSound:= 'dog.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TDog.Eat: string;
begin
Eat := 'A bone, please!';
end;
constructor TCat.Create;
begin
Kind := 'A cat';
end;
function TCat.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Mieow' ;
VoiceSound:= 'cat.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TCat.Eat: string;
begin
Eat := 'A mouse, please!';
end;
procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TAnimal.Create;
end;
procedure TFormAnimals.RbtnDogClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.RbtnCatClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TCat.Create;
end;
procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean);
var
player: TPlaySound;
begin
player := TPlaySound.Create(nil);
try
player.SoundFile := AWavFile;
if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync;
player.Execute;
finally
player.Free;
end;
end;
end.
try to execute it I getunit abstanimf;
{$MODE Delphi}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, ExtCtrls, uplaysound;
type
{ TFormAnimals }
TFormAnimals = class(TForm)
playsound1: Tplaysound;
LabelVoice: TLabel;
BtnVoice: TButton;
RbtnAnimal: TRadioButton;
RbtnDog: TRadioButton;
RbtnCat: TRadioButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure BtnVoiceClick(Sender: TObject);
procedure RbtnAnimalClick(Sender: TObject);
procedure RbtnDogClick(Sender: TObject);
procedure RbtnCatClick(Sender: TObject);
procedure PlaySound(AWavFile: String; ASync: Boolean);
private
public
{ Public declarations }
end;
TAnimal = class
public
constructor Create;
function GetKind: string;
function Voice: string; virtual;abstract;
private
Kind: string;
end;
TDog = class (TAnimal)
public
constructor Create;
function Voice: string; override;
function Eat: string; virtual;
end;
TCat = class (TAnimal)
public
constructor Create;
function Voice: string;override;
function Eat: string; virtual;
end;
var
FormAnimals: TFormAnimals;
MyAnimal: TAnimal;
implementation
{$R *.lfm}
procedure TFormAnimals.FormCreate(Sender: TObject);
begin
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.FormDestroy(Sender: TObject);
begin
MyAnimal.Free;
end;
procedure TFormAnimals.BtnVoiceClick(Sender: TObject);
begin
LabelVoice.Caption := MyAnimal.Voice;
end;
constructor TAnimal.Create;
begin
Kind := 'An animal';
end;
function TAnimal.GetKind: string;
begin
GetKind := Kind;
end;
constructor TDog.Create;
begin
Kind := 'A dog';
end;
function TDog.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Arf Arf';
VoiceSound:= 'dog.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TDog.Eat: string;
begin
Eat := 'A bone, please!';
end;
constructor TCat.Create;
begin
Kind := 'A cat';
end;
function TCat.Voice: string;
Var VoiceSound:String;
begin
Voice := 'Mieow' ;
VoiceSound:= 'cat.wav';
FormAnimals.PlaySound(VoiceSound,true);
end;
function TCat.Eat: string;
begin
Eat := 'A mouse, please!';
end;
procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TAnimal.Create;
end;
procedure TFormAnimals.RbtnDogClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TDog.Create;
end;
procedure TFormAnimals.RbtnCatClick(Sender: TObject);
begin
MyAnimal.Free;
MyAnimal := TCat.Create;
end;
procedure TFormAnimals.PlaySound(AWavFile: String; ASync: Boolean);
var
player: TPlaySound;
begin
player := TPlaySound.Create(nil);
try
player.SoundFile := AWavFile;
if Async then player.PlayStyle := psAsync else player.PlayStyle := psSync;
player.Execute;
finally
player.Free;
end;
end;
end.
It compiles with the following warnings
abstanimf.pas(136,29) Warning: Constructing a class "TAnimal" with abstract method "Voice"
abstanimf.pas(41,14) Hint: Found abstract method: Voice(<TAnimal>;<var AnsiString>):AnsiString;
When I execute it I get Class exception Runerror(211)
in line 82 LabelVoice.Caption:=MyAnimalVoice;
When I go on I get
Abstract method called.
Press OK to ignore and risk data corruption.
Press Abort to kill the program.
It is very clear that I lack some understanding regardind the classes
by I have re read the chapter in the book and I do not find my error
The LFM code is
object FormAnimals: TFormAnimals
Left = 733
Height = 233
Top = 170
Width = 278
ActiveControl = BtnVoice
Caption = 'Animals'
ClientHeight = 233
ClientWidth = 278
Color = clBtnFace
Font.CharSet = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
OnCreate = FormCreate
OnDestroy = FormDestroy
LCLVersion = '1.8.0.6'
object LabelVoice: TLabel
Left = 39
Height = 17
Top = 168
Width = 201
Alignment = taCenter
AutoSize = False
Caption = 'Voice'
Font.CharSet = ANSI_CHARSET
Font.Color = clBlack
Font.Height = -16
Font.Name = 'Arial'
Font.Style = [fsBold]
ParentColor = False
ParentFont = False
end
object BtnVoice: TButton
Left = 152
Height = 33
Top = 56
Width = 89
Caption = '&Voice'
OnClick = BtnVoiceClick
TabOrder = 0
end
object RbtnAnimal: TRadioButton
Left = 24
Height = 21
Top = 40
Width = 62
Caption = '&Animal'
OnClick = RbtnAnimalClick
TabOrder = 1
end
object RbtnDog: TRadioButton
Left = 24
Height = 21
Top = 72
Width = 46
Caption = '&Dog'
Checked = True
OnClick = RbtnDogClick
TabOrder = 2
TabStop = True
end
object RbtnCat: TRadioButton
Left = 24
Height = 21
Top = 104
Width = 43
Caption = '&Cat'
OnClick = RbtnCatClick
TabOrder = 3
end
object playsound1: Tplaysound
About.Description.Strings = (
'Plays WAVE sounds in Windows or Linux'
)
About.Title = 'PlaySound'
About.Height = 400
About.Width = 400
About.Font.Color = clNavy
About.Font.Height = -13
About.BackGroundColor = clCream
About.Version = '0.0.7'
About.Authorname = 'Gordon Bamber'
About.Organisation = 'Public Domain'
About.AuthorEmail = 'minesadorada@charcodelvalle.com'
About.ComponentName = 'PlaySound'
About.LicenseType = abModifiedGPL
PlayCommand = 'play'
left = 192
top = 124
end
end
Thanks again.This forum is my lifeboat
wp:
I wonder why you paste the code several times. And since this is a complete project why don't you pack the files into a zip and upload it? It is much easier for someone willing to help you if he can load the code into Lazarus and let Lazarus and/or the debugger do the work.
The root of the problem is here:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFormAnimals.RbtnAnimalClick(Sender: TObject);begin MyAnimal.Free; MyAnimal := TAnimal.Create;end;
TAninmal is an "abstract" class. This is because it contains an "abstract" method. This kind of methods is often defined in the very basic classes at the begin of a class hierarchy, but is implemented only in derived classes. Here the abstract method is "function Voice": Every animal (the basic class) has a voice, but every voice is different. Therefore, the derived class (the TDog, or TCat) must implement it. In TAnimal it is just something like a "forward declaration" so that the compiler knows it.
In the click procedure of BtnVoice you call this method:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFormAnimals.BtnVoiceClick(Sender: TObject);begin LabelVoice.Caption := MyAnimal.Voice;end;Voice is a "virtual" method, too. This means that the compiler uses the method which is implemented in the class created. Therefore, this code runs fine if MyAnimal has been created as TDog or TCat - these do provide an implementation for the abstract method. But if MyAnimal has been created as TAnimal as you do in the first cited code the program will crash with error 211: "Call to abstract method"
Navigation
[0] Message Index
[#] Next page