程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 操作 Wave 文件(8): 使用 TMediaPlayer 錄制 wav 文件

操作 Wave 文件(8): 使用 TMediaPlayer 錄制 wav 文件

編輯:Delphi

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. 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved