Author: Tomas Rutkauskas
I'm trying to use MediaPlayer to record sound into a wave file through a
microphone. Can someone show me some simple code to do the recording?
Answer:
The TMediaPlayer can only open a wave file that has at least one byte of data in
it. I found this out when I tried to create and open a wave file that was nothing
but a wave header. The TMediaPlayer wouldn't do it. The following code creates a
wave file with a single byte of data at the beginning. It is a bit of a kludge to
do it this way, but it works. You need to add MMSYSTEM to the uses clause of any
unit that uses this function.
1 function CreateNewWave(NewFileName: string): Boolean;
2 var
3 DeviceID: Word;
4 Return: LongInt;
5 MciOpen: TMCI_Open_Parms;
6 MciRecord: TMCI_Record_Parms;
7 MciPlay: TMCI_Play_Parms;
8 MciSave: TMCI_SaveParms;
9 MCIResult: LongInt;
10 Flags: Word;
11 TempFileName: array[0..255] of char;
12 begin
13 MediaPlayer.Close;
14 StrPCopy(TempFileName, NewFileName);
15 MciOpen.lpstrDeviceType := 'waveaudio';
16 MciOpen.lpstrElementName := '';
17 Flags := Mci_Open_Element or Mci_Open_Type;
18 MCIResult := MciSendCommand(0, MCI_OPEN, Flags, LongInt(@MciOpen));
19 DeviceID := MciOpen.wDeviceId;
20 MciRecord.dwTo := 1;
21 Flags := Mci_To or Mci_Wait;
22 MCIResult := MciSendCommand(DeviceID, Mci_Record, Flags, LongInt(@MciRecord));
23 mciPlay.dwFrom := 0;
24 Flags := Mci_From or Mci_Wait;
25 MciSendCommand(DeviceId, Mci_Play, Flags, LongInt(@MciPlay));
26 mciSave.lpfileName := TempFilename;
27 Flags := MCI_Save_File or Mci_Wait;
28 MCIResult := MciSendCommand(DeviceID, MCI_Save, Flags, LongInt(@MciSave));
29 Result := MciSendCommand(DeviceID, Mci_Close, 0, LongInt(nil)) = 0;
30 end;
|