delphi制作wav文件的方法
来源:本站原创|时间:2020-01-10|栏目:Delphi|点击: 次
本文实例讲述了delphi制作wav文件的方法。分享给大家供大家参考。具体如下:
这里delphi用waveIn...函数制作wav文件
具体代码如下:
//使用窗口接受音频设备发出的消息: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); protected procedure WndProc(var m: TMessage); override; end; var Form1: TForm1; implementation {$R *.dfm} uses MMSystem; var whIn1,whIn2, whOut: TWaveHdr; hWaveIn,hWaveOut: HWAVE; fmt: TWaveFormatEx; buf1,buf2,SaveBuf: TBytes; procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption := '开始录音'; Button2.Caption := '停止录音'; Button3.Caption := '播放录音'; end; //开始录音 procedure TForm1.Button1Click(Sender: TObject); begin {指定要录制的格式} fmt.wFormatTag := WAVE_FORMAT_PCM; fmt.nChannels := 2; fmt.nSamplesPerSec := 22050; fmt.nAvgBytesPerSec := 88200; fmt.nBlockAlign := 4; fmt.wBitsPerSample := 16; fmt.cbSize := 0; SaveBuf := nil; {清除已录制的内容} if waveInOpen(@hWaveIn, WAVE_MAPPER, @fmt, Handle, 0, CALLBACK_WINDOW) = 0 then begin SetLength(buf1, 1024*8); SetLength(buf2, 1024*8); whIn1.lpData := PAnsiChar(buf1); whIn1.dwBufferLength := Length(buf1); whIn1.dwBytesRecorded := 0; whIn1.dwUser := 0; whIn1.dwFlags := 0; whIn1.dwLoops := 0; whIn1.lpNext := nil; whIn1.reserved := 0; whIn2.lpData := PAnsiChar(buf2); whIn2.dwBufferLength := Length(buf2); whIn2.dwBytesRecorded := 0; whIn2.dwUser := 0; whIn2.dwFlags := 0; whIn2.dwLoops := 0; whIn2.lpNext := nil; whIn2.reserved := 0; waveInPrepareHeader(hWaveIn, @whIn1, SizeOf(TWaveHdr)); waveInPrepareHeader(hWaveIn, @whIn2, SizeOf(TWaveHdr)); waveInAddBuffer(hWaveIn, @whIn1, SizeOf(TWaveHdr)); waveInAddBuffer(hWaveIn, @whIn2, SizeOf(TWaveHdr)); waveInStart(hWaveIn); end; end; //停止录音 procedure TForm1.Button2Click(Sender: TObject); begin waveInStop(hWaveIn); waveInUnprepareHeader(hWaveIn, @whIn1, SizeOf(TWaveHdr)); waveInUnprepareHeader(hWaveIn, @whIn2, SizeOf(TWaveHdr)); waveInClose(hWaveIn); end; //播放录音 procedure TForm1.Button3Click(Sender: TObject); begin whOut.lpData := PAnsiChar(SaveBuf); whOut.dwBufferLength := Length(SaveBuf); whOut.dwBytesRecorded := 0; whOut.dwUser := 0; whOut.dwFlags := 0; whOut.dwLoops := 1; whOut.lpNext := nil; whOut.reserved := 0; waveOutOpen(@hWaveOut, WAVE_MAPPER, @fmt, Handle, 0, CALLBACK_WINDOW); waveOutPrepareHeader(hWaveOut, @whOut, SizeOf(TWaveHdr)); waveOutWrite(hWaveOut, @whOut, SizeOf(TWaveHdr)); end; procedure TForm1.WndProc(var m: TMessage); var ordLen: Integer; begin inherited; case m.Msg of {处理录音消息} MM_WIM_OPEN: ; {此消息只携带了设备句柄} MM_WIM_CLOSE: ; {此消息只携带了设备句柄} MM_WIM_DATA: begin {此消息携带了设备句柄和 WaveHdr 指针(LParam)} {保存录制的数据} ordLen := Length(SaveBuf); SetLength(SaveBuf, ordLen + PWaveHdr(m.LParam).dwBytesRecorded); CopyMemory(Ptr(DWORD(SaveBuf)+ordLen), PWaveHdr(m.LParam).lpData, PWaveHdr(m.LParam).dwBytesRecorded); {继续录制} waveInAddBuffer(hWaveIn, PWaveHdr(m.LParam), SizeOf(TWaveHdr)); end; {处理播放消息} MM_WOM_OPEN: ; {此消息只携带了设备句柄} MM_WOM_CLOSE: ; {此消息只携带了设备句柄} MM_WOM_DONE: begin {此消息携带了设备句柄和 WaveHdr 指针(LParam)} waveOutUnprepareHeader(hWaveOut, PWaveHdr(m.LParam), SizeOf(TWaveHdr)); waveOutClose(hWaveOut); end; end; end; end.
希望本文所述对大家的Delphi程序设计有所帮助。
您可能感兴趣的文章
- 01-10在Delphi实现在数据库中存取图像的图文演示无错
- 01-10delphi建立、读取、存贮INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲线图
- 01-10delphi建立、读取、存贮INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判断文件是否为文本文件的函数
- 01-10delphi中一个值得大家来考虑的DLL问题
- 01-10初探Delphi中的插件编程
阅读排行
本栏相关
- 01-10在Delphi实现在数据库中存取图像的图
- 01-10delphi建立、读取、存贮INI文件的方法
- 01-10delphi 正弦曲线图
- 01-10Delphi Command模式
- 01-10delphi建立、读取、存贮INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判断文件是否为文本文件的函
- 01-10delphi中一个值得大家来考虑的DLL问题
- 01-10初探Delphi中的插件编程
随机阅读
- 01-11ajax实现页面的局部加载
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05dedecms(织梦)副栏目数量限制代码修改