Delphi 播放wav声音
生活随笔
收集整理的這篇文章主要介紹了
Delphi 播放wav声音
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. uVar.pas單元如下
unit uVar;interfaceusesSysUtils, Forms;typeTAppPara = classpublicclass function AppPath: string;class function AppName: string;end;TFilePath = class(TAppPara)publicclass function VoicePath: string;end;implementation{ TFilePath }class function TFilePath.VoicePath: string; beginResult := AppPath + 'voice\'; end;{ TAppPara }class function TAppPara.AppName: string; beginResult := ExtractFileName(Application.ExeName); end;class function TAppPara.AppPath: string; beginResult := ExtractFilePath(Application.ExeName); end;end.2. uVoice.pas單元如下
unit uVoice;interfaceusesSysUtils, MMSystem;constFileExt= '.wav';type//播放wav文件TVoice= classprivateFFilePath: string; //wav文件目錄publicproperty _FilePath: string read FFilePath write FFilePath;constructor Create(sPath: string);destructor Destroy; override;function Play(sFile: string): Boolean;end;implementation{ TVoice }constructor TVoice.Create(sPath: string); beginFFilePath:= sPath; end;destructor TVoice.Destroy; begininherited; end;function TVoice.Play(sFile: string): Boolean; varfilename: string; beginResult:= False;tryfilename:= FFilePath+ sFile+ FileExt;if FileExists(filename) thenResult:= sndPlaySound(PChar(filename), SND_NODEFAULT+ SND_ASYNC);{SND_ASYNC 用異步方式播放聲音SND_LOOP 播放載入到內存中的聲音SND_MEMORY 播放載入到內存中的聲音SND_NODEFAULT 不播放缺省聲音SND_NOWAIT 如果驅動程序正忙則函數就不播放聲音并立即返回SND_PURGE 停止所有的聲音SND_SYNC 同步播放聲音,在播放完后PlaySound函數才返回}finally//end; end;end.3. uFrmMain.pas如下
unit uFrmMain;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, OleServer, SpeechLib_TLB, StdCtrls, uVoice;typeTFrmMain = class(TForm)Button1: TButton;Button2: TButton;Label1: TLabel;Edit1: TEdit;SpVoice1: TSpVoice;procedure Button1Click(Sender: TObject);procedure Button2Click(Sender: TObject);procedure FormCreate(Sender: TObject);procedure FormDestroy(Sender: TObject);private{ Private declarations }FVoice: TVoice;public{ Public declarations }end;varFrmMain: TFrmMain;implementationuses uVar;{$R *.dfm}procedure TFrmMain.Button1Click(Sender: TObject); varsName: string; begin//語音文件名sName:= '歡迎光臨';if Assigned(FVoice) thenFVoice.Play(sName); end;procedure TFrmMain.Button2Click(Sender: TObject); varsName: string; begin//可能有問題 // sName:= Trim(Edit1.Text); // SpVoice1.Speak(sName, SVSFlagsAsync); end;procedure TFrmMain.FormCreate(Sender: TObject); beginFVoice:= TVoice.Create(TFilePath.VoicePath); end;procedure TFrmMain.FormDestroy(Sender: TObject); beginFreeAndNil(FVoice); end;end.4. 工程文件
program DelphiVoice;usesForms,uFrmMain in 'uFrmMain.pas' {FrmMain},uVoice in 'uVoice.pas',uVar in 'uVar.pas';{$R *.res}beginApplication.Initialize;Application.MainFormOnTaskbar := True;Application.CreateForm(TFrmMain, FrmMain);Application.Run; end.?
總結
以上是生活随笔為你收集整理的Delphi 播放wav声音的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux窗口字是倒着的,linux反撇
- 下一篇: bigemap软件功能对比