我在网上试了很多例子都不能发出声,请高人指点:
例子:1)编写.RC文件
..RC文件是资源的源文件,编译器也就编译这个文件,生成.RES的资源文件 首先在我们的项目子目录中建立一个纯文本文件,起名叫pad.rc ,在文件中写一行,内容为://注意!资源文件名不可以和WAVE文件名一样的!SOUND1 WAV SOUND.WAV其中SOUND.WAV为一个Windows下普通的声音文件
2)编译它在DOS的提示符下打 BRCC32 PAD.RC 一会儿后,就编译完了
3)制作程序
在WINDOWS下启动DELPHI,新建一个项目,将其中的文件保存到我们的项目中的子目录中。 在Unit1.pas中找到
{$R *.DFM}
把我们的资源文件就声明在后面
{$R PAD.RES}
然后,在Form1中声明两个全局变量PtrSound : PChar;
hRes : THandle; {handle to the loaded resource
if 0 indicates nothing playing}再在Form1的Create事件中写下如下代码procedure TForm1.FormCreate(Sender: TObject);
var
hResInfo : THandle;
begin
hResInfo := FindResource(HInstance, 'SOUND1', 'WAVE');
hRes := LoadResource(HInstance, hResInfo);
if hRes > 32 then {its a good load}
begin {lock the resource}
ptrSound:=LockResource(hRes);
end;
end;
然后在Form1中放一个按钮Button1,写如下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
sndplaysound(ptrSound,snd_async or snd_Memory);
end; /*点击编译,运行,再按BUTTON1,可以听到扬声器中发出WAVE文件内容。 */为什么我的程序不能播放呢?
例子:1)编写.RC文件
..RC文件是资源的源文件,编译器也就编译这个文件,生成.RES的资源文件 首先在我们的项目子目录中建立一个纯文本文件,起名叫pad.rc ,在文件中写一行,内容为://注意!资源文件名不可以和WAVE文件名一样的!SOUND1 WAV SOUND.WAV其中SOUND.WAV为一个Windows下普通的声音文件
2)编译它在DOS的提示符下打 BRCC32 PAD.RC 一会儿后,就编译完了
3)制作程序
在WINDOWS下启动DELPHI,新建一个项目,将其中的文件保存到我们的项目中的子目录中。 在Unit1.pas中找到
{$R *.DFM}
把我们的资源文件就声明在后面
{$R PAD.RES}
然后,在Form1中声明两个全局变量PtrSound : PChar;
hRes : THandle; {handle to the loaded resource
if 0 indicates nothing playing}再在Form1的Create事件中写下如下代码procedure TForm1.FormCreate(Sender: TObject);
var
hResInfo : THandle;
begin
hResInfo := FindResource(HInstance, 'SOUND1', 'WAVE');
hRes := LoadResource(HInstance, hResInfo);
if hRes > 32 then {its a good load}
begin {lock the resource}
ptrSound:=LockResource(hRes);
end;
end;
然后在Form1中放一个按钮Button1,写如下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
sndplaysound(ptrSound,snd_async or snd_Memory);
end; /*点击编译,运行,再按BUTTON1,可以听到扬声器中发出WAVE文件内容。 */为什么我的程序不能播放呢?
使用过C++的朋友们都知道,资源在Windows编程里非常重要,合理地运用资源可以使程序更加灵活。其实在Delphi里,同样也可以制作资源,只不过Delphi本 身对可视的编程强调很多,对资源的制作就没有多加解释,也没有象Borland C++ 里提供了资源制作工具Resource WorkShop,这些都没有关系,下面我就通过将WAV文件做成资源,从而编译进EXE文件来介绍一下如何利用Delphi本身提供的工具制作资源。
16位的Delphi 1.0和32位的Delphi2.0、3.0都提供了资源编译工具,其中 Delphi 1.0的资源编译器叫BRCC.EXE,Delphi 2.0的资源编译器叫BRCC32.EXE 用来编译32位资源,所有资源编译器都只提供了命令行版本,没有提供Windows 版本。制作一个资源一般要通过以下几个步骤:
1)编写.RC文件
..RC文件是资源的源文件,编译器也就编译这个文件,生成.RES的资源文件 首先在我们的项目子目录中建立一个纯文本文件,起名叫Sound.rc,文件中 有一行,内容为:
SOUND1 WAV SOUND.WAV
其中SOUND.WAV为一个Windows下普通的声音文件
2)编译它
在DOS的提示符下打 BRCC SOUND.RC 硬盘哗啦啦转一会儿后,就编
译完了
3)制作程序
这也是最复杂,最灵活的一步,首先启动Windows, 再启动Delphi,
并且将项目 中的文件保存到我们的项目中的子目录中。 在Unit1.pas中找这么一行
{$R *.DFM}
把我们的资源文件就声明在后面
{$R SOUND.RES}
然后,在Form1中声明两个全局变量
PtrSound : PChar;
hRes : THandle; {handle to the loaded resource if 0 indicates nothing playing} 再在Form1的Create事件中写下如下代码
procedure TForm1.FormCreate(Sender: TObject);
var hResInfo : THandle;
begin
hResInfo := FindResource(HInstance, 'SOUND1', 'WAVE');
hRes := LoadResource(HInstance, hResInfo);
if hRes > 32 then {its a good load}
begin {lock the resource}
ptrSound:=LockResource(hRes);
end;
end; 然后在Form1中放一个按钮Button1,写如下代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
sndplaysound(ptrSound,snd_async or snd_Memory);
end;
这时候就可以运行了,当然,这只是一个例子,只是提供了调用资源的 一种方法,距离实际应用还有一段距离。愿这个Tip能给大家起个抛砖引玉的作用。
你是看着这一篇文章把
wave文件格式说明
unit LinearSystem;interface{============== WAV Format Coding Type ==================}type WAVHeader = recordnChannels : Word;nBitsPerSample : LongInt;nSamplesPerSec : LongInt;nAvgBytesPerSec : LongInt;RIFFSize : LongInt;fmtSize : LongInt;formatTag : Word;nBlockAlign : LongInt;DataSize : LongInt;end;{============== Sample DataStreams ========================}const MaxN = 300; { max number of sample values }type SampleIndex = 0 .. MaxN+3;type DataStream = array[ SampleIndex ] of Real;var N : SampleIndex;{============== Observation Variables ======================}type Observation = recordName : String[40]; {Name of this observation}yyy : DataStream; {Array of data points}WAV : WAVHeader; {WAV specs for observation}Last : SampleIndex;{Last valid index to yyy}MinO, MaxO : Real; {Range values from yyy}end;var K0R, K1R, K2R, K3R : Observation;K0B, K1B, K2B, K3B : Observation;{================== File Name Variables ===================}var StandardDatabase : String[ 80 ];BaseFileName : String[ 80 ];StandardOutput : String[ 80 ];StandardInput : String[ 80 ];{=============== Operations ==================}procedure ReadWAVFile (var Ki, Kj : Observation);procedure WriteWAVFile(var Ki, Kj : Observation);procedure ScaleData (var Kk : Observation);procedure InitAllSignals;procedure InitLinearSystem; implementation{$R *.DFM}uses VarGraph, SysUtils; {================== Standard WAV File Format ===================}const MaxDataSize : LongInt = (MaxN+1)*2*2;const MaxRIFFSize : LongInt = (MaxN+1)*2*2+36;const StandardWAV : WAVHeader = (nChannels : Word(2);nBitsPerSample : LongInt(16);nSamplesPerSec : LongInt(8000);nAvgBytesPerSec : LongInt(32000);RIFFSize : LongInt((MaxN+1)*2*2+36);fmtSize : LongInt(16);formatTag : Word(1);nBlockAlign : LongInt(4);DataSize : LongInt((MaxN+1)*2*2)); {================== Scale Observation Data ===================}procedure ScaleData(var Kk : Observation);var I : SampleIndex;begin{Initialize the scale values}Kk.MaxO := Kk.yyy[0];Kk.MinO := Kk.yyy[0];{Then scan for any higher or lower values}for I := 1 to Kk.Last dobeginif Kk.MaxO < Kk.yyy[I] then Kk.MaxO := Kk.yyy[I];if Kk.MinO > Kk.yyy[I] then Kk.MinO := Kk.yyy[I];end;end; { ScaleData }procedure ScaleAllData;beginScaleData(K0R);ScaleData(K0B);ScaleData(K1R);ScaleData(K1B);ScaleData(K2R);ScaleData(K2B);ScaleData(K3R);ScaleData(K3B);end; {ScaleAllData}{================== WAV Data I/O ===================}VAR InFile, OutFile : file of Byte;type Tag = (F0, T1, M1);type FudgeNum = recordcase X:Tag ofF0 : (chrs : array[0..3] of Byte);T1 : (lint : LongInt);M1 : (up,dn: Integer);end;var ChunkSize : FudgeNum;procedure WriteChunkName(Name:String);var i : Integer;MM : Byte;beginfor i := 1 to 4 dobeginMM := ord(Name[i]);write(OutFile,MM);end;end; {WriteChunkName}procedure WriteChunkSize(LL:Longint);var I : integer;beginChunkSize.x:=T1;ChunkSize.lint:=LL;ChunkSize.x:=F0;for I := 0 to 3 do Write(OutFile,ChunkSize.chrs[I]);end;procedure WriteChunkWord(WW:Word);var I : integer;beginChunkSize.x:=T1;ChunkSize.up:=WW;ChunkSize.x:=M1;for I := 0 to 1 do Write(OutFile,ChunkSize.chrs[I]);end; {WriteChunkWord}procedure WriteOneDataBlock(var Ki, Kj : Observation);var I : Integer;beginChunkSize.x:=M1;with Ki.WAV dobegincase nChannels of1:if nBitsPerSample=16then begin {1..2 16-bit samples in buffer for one channel}ChunkSize.up := trunc(Ki.yyy[N]+0.5);if N .5);N := N+2;endelse begin {1..4 8-bit samples in buffer for one channel}for I:=0 to 3 do ChunkSize.chrs[I]:= trunc(Ki.yyy[N+I]+0.5);N := N+4;end;2:if nBitsPerSample=16then begin {2 16-bit samples on two channels}ChunkSize.dn := trunc(Ki.yyy[N]+0.5);ChunkSize.up := trunc(Kj.yyy[N]+0.5);N := N+1;endelse begin {4 8-bit samples on two channels}ChunkSize.chrs[1] := trunc(Ki.yyy[N]+0.5);ChunkSize.chrs[3] := trunc(Ki.yyy[N+1]+0.5);ChunkSize.chrs[0] := trunc(Kj.yyy[N]+0.5);ChunkSize.chrs[2] := trunc(Kj.yyy[N+1]+0.5);N := N+2;end;end; {with WAV do begin..}end; {the four-byte variable "ChunkSize" has now been filled}ChunkSize.x:=T1;WriteChunkSize(ChunkSize.lint);{put 4 bytes of data}end; {WriteOneDataBlock}procedure WriteWAVFile(var Ki, Kj : Observation);var MM : Byte;I : Integer;OK : Boolean;begin{Prepare to write a file of data}AssignFile(OutFile, StandardOutput); { File selected in dialog }ReWrite( OutFile );With Ki.WAV dobegin DataSize := nChannels*(nBitsPerSample div 8)*(Ki.Last+1);RIFFSize := DataSize+36;fmtSize := 16;end;{Write ChunkName "RIFF"}WriteChunkName('RIFF');{Write ChunkSize}WriteChunkSize(Ki.WAV.RIFFSize);{Write ChunkName "WAVE"}WriteChunkName('WAVE');{Write tag "fmt_"}WriteChunkName('fmt ');{Write ChunkSize}Ki.WAV.fmtSize := 16; {should be 16-18}WriteChunkSize(Ki.WAV.fmtSize);{Write formatTag, nChannels}WriteChunkWord(Ki.WAV.formatTag);WriteChunkWord(Ki.WAV.nChannels);{Write nSamplesPerSec}WriteChunkSize(Ki.WAV.nSamplesPerSec);{Write nAvgBytesPerSec}WriteChunkSize(Ki.WAV.nAvgBytesPerSec);{Write nBlockAlign, nBitsPerSample}WriteChunkWord(Ki.WAV.nBlockAlign);WriteChunkWord(Ki.WAV.nBitsPerSample);{WriteDataBlock tag "data"}WriteChunkName('data');{Write DataSize}WriteChunkSize(Ki.WAV.DataSize);N:=0; {first write-out location}while N<=Ki.Last do WriteOneDataBlock(Ki,Kj); {put 4 bytes & increment N}{Free the file buffers}CloseFile( OutFile );end; {WriteWAVFile}procedure InitSpecs;beginend; { InitSpecs }procedure InitSignals(var Kk : Observation);var J : Integer;beginfor J := 0 to MaxN do Kk.yyy[J] := 0.0;Kk.MinO := 0.0;Kk.MaxO := 0.0;Kk.Last := MaxN;end; {InitSignals}
各位兄弟有没有简单的源代码和编译后的程序打包发给我,
将不胜感激并高分相送!
Email : [email protected]