1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| { Unit lzcsound
Copyright (C) 2009 Lazarus-fr Team
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
Author: Serge Girard
Abstract:
Two components to play wav files
Requirements: FPC 2.2.4
}
unit lzcsound;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,PropEdits, Dialogs, FileUtil, LResources,LZCBase;
type
TSoundFileNamePropertyEditor = class(TFileNamePropertyEditor)
public
function GetDialogTitle : string; override;
function GetFilter: string; override;
function GetInitialDirectory: string; override;
procedure Edit; override;
end;
TLZCWave = class(TLZCustomComponent)
protected
FSoundFile : String;
FOnPlay : TNotifyEvent;
private
procedure SetSoundFile(const AValue: string);
public
procedure Play;
published
property OnPlay : TNotifyEvent read FOnPlay write FOnPlay;
property SoundFile : String read FSoundFile write SetSoundFile;
end;
TLZClrsWave = class(TLZCustomComponent)
protected
FLrsSoundName : String;
FVolatile : Boolean;
FOnPlay : TNotifyEvent;
public
procedure Play;
published
property OnPlay : TNotifyEvent read FOnPlay write FOnPlay;
property LrsSoundName : String read FLrsSoundName write FLrsSoundName;
property Volatile : Boolean read FVolatile write FVolatile;
end;
{$IFDEF WINDOWS}
SoundFlags =(
SND_SYNC = $0000, // play synchronously (default) */
SND_ASYNC = $0001, // play asynchronously */
SND_NODEFAULT = $0002, // silence (!default) if sound not found */
SND_MEMORY = $0004, // pszSound points to a memory file */
SND_LOOP = $0008, // loop the sound until next sndPlaySound */
SND_NOSTOP = $0010, // don't stop any currently playing sound */
SND_NOWAIT = $00002000, // don't wait if the driver is busy */
SND_ALIAS = $00010000, // name is a registry alias */
SND_ALIAS_ID = $00110000, // alias is a predefined ID */
SND_FILENAME = $00020000, // name is file name */
SND_RESOURCE = $00040004 // name is resource name or atom */
);
{$ENDIF}
{$IFDEF WINCE}
function WinCEPlaySound(x1: PWideChar; x2: HMODULE; x3: DWORD): longbool;stdcall; external 'coredll.dll' name 'PlaySoundW';
{$ENDIF}
{$IFDEF MSWINDOWS}
function WindowsPlaySound(pszSound: PChar; hmod: HMODULE; fdwSound: DWORD): BOOLean; stdcall; external 'winmm.DLL' name 'PlaySound';
{$ENDIF}
procedure Register;
resourcestring
rsDialogTitle = 'A la recherche du Son Perdu';
rsDialogFilter = 'Fichiers WAV|*.wav';
implementation
uses LazIDEIntf;
procedure Register;
begin
RegisterComponents('LZSound',[TLZCWave,TLZClrsWave]);
RegisterPropertyEditor(TypeInfo(string), TLZCWave, 'SoundFile',TSoundFileNamePropertyEditor);
end;
{TLZCWave}
procedure TLZCWave.Play;
begin
if Assigned(FOnPlay) then OnPlay(self);
{$IFDEF WINDOWS}
{$IFDEF WINCE}
WinCEPlaysound(FSoundFile,0,0);
{$ELSE}
WindowsPlaysound(@FSoundFile[1],0, LongWord(SND_FILENAME));
{$ENDIF}
{$ENDIF}
{$IFDEF LINUX}
SysUtils.ExecuteProcess(FindDefaultExecutablePath('paplay'),FSoundFile);
{$ENDIF}
end;
procedure TLZCWave.SetSoundFile(const AValue: string);
var
fn: string;
begin
if (FSoundFile = AValue) then
exit;
FSoundFile := AValue;
if (FSoundFile = '') then
exit;
if (csDesigning in ComponentState) then
begin
fn := ExtractFileName(AValue);
FSoundFile := ExtractFilePath(AValue);
FSoundFile := FSoundFile + fn;
end
else
begin
FSoundFile := AValue;
end;
end;
{TLZClrsWave}
procedure TLZClrsWave.Play;
var FileName : String;
Stream: TFileStream;
begin
FileName:=SysUtils.GetTempDir(False)+FLrsSoundName+'.wav';
Stream := TFileStream.Create(FileName, fmCreate);
with LazarusResources.Find(FLrsSoundName) do
Stream.Write(PChar(Value)^, Length(Value));
Stream.Free;
if Assigned(FOnPlay) then OnPlay(self);
{$IFDEF WINDOWS}
{$IFDEF WINCE}
WinCEPlaysound(FileName,0,0);
{$ELSE}
WindowsPlaysound(@FileName[1],0, $00020000);
{$ENDIF}
{$ENDIF}
{$IFDEF LINUX}
SysUtils.ExecuteProcess(FindDefaultExecutablePath('paplay'),FileName);
{$ENDIF}
if FVolatile then DeleteFile(FileName);
end;
{ TSoundFileNamePropertyEditor }
function TSoundFileNamePropertyEditor.GetFilter: string;
begin
Result := rsDialogFilter;
end;
function TSoundFileNamePropertyEditor.GetDialogTitle: string;
begin
Result := rsDialogTitle;
end;
function TSoundFileNamePropertyEditor.GetInitialDirectory: string;
begin
Result := ExtractFilePath(LazarusIDE.ActiveProject.ProjectInfoFile);
end;
procedure TSoundFileNamePropertyEditor.Edit;
begin
With CreateFileDialog do
Try
Filter:=rsDialogFilter;
Options:=GetDialogOptions;
FileName:=GetStrValue;
InitialDir:=GetInitialDirectory;
Title:=GetDialogTitle;
If Execute then
SetFilename(Filename);
Finally
Free;
end;
end;
end. |
Partager