(8): 使用 TMediaPlayer 录制 wav 文件

TMediaPlayer 录音是基于一个已存在的 wav 文件, 上次建立空白 wav 的函数可派上用场了.

  TMediaPlayer 的功能是基于 MCI 的, 都是该淘汰的东西了, 只是简单了解下.

  接下来还要学习用 waveIn...系列函数录音、用 DirectSound 录音.

unit Unit1; 
 
interface 
 
uses 
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
 Dialogs, MPlayer, StdCtrls; 
 
type 
 TForm1 = class(TForm) 
  MediaPlayer1: TMediaPlayer; 
  Button1: TButton; 
  Button2: TButton; 
  procedure FormCreate(Sender: TObject); 
  procedure Button1Click(Sender: TObject); 
  procedure Button2Click(Sender: TObject); 
 end; 
 
var 
 Form1: TForm1; 
 
implementation 
 
{$R *.dfm}  
 
uses MMSystem; 
 
//建立一个空白 Wave 文件的函数 
function CreateWav(chan, freq, bit: Word; const FilePath: string): Boolean; 
var 
 h: HMMIO; 
 ckiRiff, ckiFmt, ckiData: TMMCKInfo; 
 fmt: TPCMWaveFormat; 
begin 
 ZeroMemory(@ckiRiff, SizeOf(TMMCKInfo)); 
 ckiRiff.cksize := 36; 
 ckiRiff.fccType := mmioStringToFOURCC('WAVE', 0); 
 
 ZeroMemory(@ckiFmt, SizeOf(TMMCKInfo)); 
 ckiFmt.ckid := mmioStringToFOURCC('fmt', 0); 
 
 ZeroMemory(@ckiData, SizeOf(TMMCKInfo)); 
 ckiData.ckid := mmioStringToFOURCC('data', 0); 
 
 fmt.wf.wFormatTag := WAVE_FORMAT_PCM; 
 fmt.wf.nChannels := chan; 
 fmt.wf.nSamplesPerSec := freq; 
 fmt.wf.nAvgBytesPerSec := freq * chan * bit div 8; 
 fmt.wf.nBlockAlign := chan * bit div 8; 
 fmt.wBitsPerSample := bit; 
 
 h := mmioOpen(PChar(FilePath), nil, MMIO_CREATE or MMIO_WRITE); 
 if h = 0 then Exit(False); 
 
 if (mmioCreateChunk(h, @ckiRiff, MMIO_CREATERIFF) = MMSYSERR_NOERROR) and 
  (mmioCreateChunk(h, @ckiFmt, 0) = MMSYSERR_NOERROR) and 
  (mmioWrite(h, PAnsiChar(@fmt), SizeOf(TPCMWaveFormat)) = SizeOf(TPCMWaveFormat)) and 
  (mmioAscend(h, @ckiFmt, 0) = MMSYSERR_NOERROR) and 
  (mmioCreateChunk(h, @ckiData, 0) = MMSYSERR_NOERROR) then Result := True; 
 
 mmioClose(h, 0); 
end; 
 
//文件路径 
const path = 'C:/Temp/Test.wav'; 
 
//开始录音 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
 CreateWav(2, 22050, 16, path); 
 
 MediaPlayer1.FileName := path; 
 MediaPlayer1.Open; 
 MediaPlayer1.StartRecording; 
 
 Button2.Enabled := True; 
end; 
 
//停止录音并播放 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
 MediaPlayer1.Stop; 
 MediaPlayer1.Play; 
end; 
 
procedure TForm1.FormCreate(Sender: TObject); 
begin 
 MediaPlayer1.Visible := False; 
 Button2.Enabled := FileExists(path); 
end; 
 
end. 

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值