darkdog
darkdog

Reputation: 4015

Recording and saving .wav Files with TMediaPlayer (Delphi 2010)

well im really new to the delphi world.

Right now im using the TMediaPlayer to record some sound and save those. I made a click event for the record button in the TMediaPlayer which executes a SaveFileDialog. The user should type in some filename he would like to save and then after recording the .wav file he can click on the stop button and it will save his recorded .wav file.

Actually it dont event create a file.

I will show some important code parts of my delphi code

if Button = TMPBtnType.btRecord then
begin
SaveDialogSpeichern.Execute;
MediaPlayerSound.FileName := SaveDialogSpeichern.FileName;
MediaPlayerSound.StartRecording;

end

and those for save:

  MediaPlayerSound.Stop;
  MediaPlayerSound.Save;

I cant use the bass.dll , so i would like to make this with the TMediaPlayer if there is a possible way

Upvotes: 0

Views: 5313

Answers (3)

Logan_Cale
Logan_Cale

Reputation: 11

I've solved your problem with TMediaPlayer : for recording you have to add the "open" command like this :

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    this->SaveDialog1->Execute();
    this->MediaPlayer1->FileName = this->SaveDialog1->FileName;
    this->MediaPlayer1->Open();
    this->MediaPlayer1->StartRecording();
}

Warning : TMediaPlayer do not support creating sound files (as you said), so you must write a new file manually before using it ( weired ).

You can do it manually (just create a new .wav somewhere) or using this code : (I recommand to use this code if you do not want to record on an existing file : )

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    this->SaveDialog1->Execute();
    this->MediaPlayer1->FileName = this->SaveDialog1->FileName;

    TMemoryStream *AudioFile;
    AudioFile = new TMemoryStream();
    char buffer[43]= 0x52,0x49,0x46,0x46,0x04,0xc0,0x50,0x00,0x57,0x41,0x56,0x45,0x66,0x6D,0x74,0x20,
    0x10,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x44,0xAC,0x00,0x00,0x88,0x58,0x01,0x00,
    0x02,0x00,0x10,0x00,0x64,0x61,0x74,0x61,0xE0,0xBF,0x50}; // .wav header example
    AudioFile->Write(buffer,43);
    char byte=0;
    for (int i = 0; i < 49120; i++) // some free audiodata
    {
        AudioFile->Write(&byte,1);
    }
    AudioFile->SaveToFile(this->SaveDialog1->FileName);
    AudioFile->Clear();
    delete AudioFile;
    AudioFile=NULL;

    this->MediaPlayer1->Open();
    this->MediaPlayer1->StartRecording();

}

// stop button :

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    this->MediaPlayer1->Stop();
}

//save button :

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    this->MediaPlayer1->Save();
    this->MediaPlayer1->Close();
}

Upvotes: 0

mehmet fatih barut
mehmet fatih barut

Reputation: 21

And also I want to add this; you can start stop and save your record using this way

procedure TForm1.Button1Click(Sender: TObject);     // Record
begin
  mciSendString('OPEN NEW TYPE WAVEAUDIO ALIAS mysound', nil, 0, Handle);
  mciSendString('SET mysound TIME FORMAT MS ' +     // set time
    'BITSPERSAMPLE 8 ' +                // 8 Bit
    'CHANNELS 1 ' +                     // MONO
    'SAMPLESPERSEC 8192 ' +             // 8 KHz
    'BYTESPERSEC 8192',                // 8000 Bytes/s
    nil, 0, Handle);
  mciSendString('RECORD mysound', nil, 0, Handle);
end;


procedure TForm1.Button2Click(Sender: TObject);     // Stop
begin
  mciSendString('STOP mysound', nil, 0, Handle)
end;


procedure TForm1.Button3Click(Sender: TObject);      // Save

begin
mciSendString(PChar('SAVE mysound "' + '/test.wav'+'"' ), nil, 0,
    Handle);
  mciSendString('CLOSE mysound', nil, 0, Handle)
end;

Upvotes: 2

darkdog
darkdog

Reputation: 4015

I found a solution for my problem. It seems that the TMediaPlayer do not support creating sound files either Recording sound files.

There is a way to use the WinApi (using mmSystem;)

i used this code:

    mciSendString(PChar('OPEN NEW TYPE WAVEAUDIO ALIAS mysound'), nil, 0,
    Handle);
 mciSendString(PChar('SET mysound TIME FORMAT MS ' +     
   'BITSPERSAMPLE 8 ' +                
   'CHANNELS 1 ' +                     
   'SAMPLESPERSEC 8000 ' +             
   'BYTESPERSEC 8000'),                
   nil, 0, Handle);
 mciSendString(PChar('RECORD mysound'), nil, 0, Handle);
mciSendString(PChar('SAVE mysound "' + SaveDialogSpeichern.FileName+'"' ), nil, 0,
    Handle);
mciSendString(PChar('CLOSE mysound'), nil, 0, Handle) 

hope this will help anyone with the same problem

Upvotes: 2

Related Questions