Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recording/Overwriting Waves with TMediaPlayer

Posted on 1999-07-29
7
Medium Priority
?
334 Views
Last Modified: 2010-04-04
I am using TMediaPlayer to record Wavefiles, and everything works fine except that when the Wavefile already exists, the recording is appended to the original wave rather than overwriting it.  Setting StartPos will not work because the recorded sound may be shorter than the original sound, and there would be leftovers at the end of the new recording.  I s there an easy solution to this that I'm overlooking?  I tried overwriting the wavefile with a zero-length file, but it gives an error (not valid wavefile).  Thanks for any help!
0
Comment
Question by:jsexton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 1391867
hi,
do this first:

if fileexists('c:\my.wav')
then deletefile('c:\my.wav');

Regards Barry
0
 
LVL 17

Expert Comment

by:inthe
ID: 1391868
actually it would be:

if fileexists(MediaPlayer1.Filename)
then deletefile(MediaPlayer1.Filename);

0
 

Author Comment

by:jsexton
ID: 1391869
Barry,

    Actually I tried that, but I get an error on open (file does not exist).  I tried removing the open statement, but then I get an error on StartRecording (device not open).  Here's what I'm trying to do:

  WavePlayer.filename := 'c:\my.wav';
  DeleteFile(WavePlayer.filename);
  WavePlayer.open;

  //returns error here (cannot open - does not exist)

  WavePlayer.StartRecording;

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:inthe
ID: 1391870
ok im going to do a test.
0
 
LVL 17

Expert Comment

by:inthe
ID: 1391871
mmm..ok i see what you eman.
this is because the mediaplayer cant create a new file ...so i went digging sround and found this ,i hop it helps you out.:

This document describes the process for creating added
functionality ,that many Delphi users have requested,
to the TMediaPlayer. The new functionality is the ability
to create a new file with the .wav format when recording.
The procedure "SaveMedia" creates a record type that is
passed to the MCISend command. There is an appexception
that calls close media if any error occurs while attempting
to open the specified file. The application consists two
buttons. Button1 calls the OpenMedia and RecordMedia
procedures in that order.The CloseMedia procedure is called
whenever an exception is generated in this application.
Button2 calls the StopMedia,SaveMedia, and CloseMedia
procedures.


unit utestrec;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs,MPlayer,MMSystem,StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AppException(Sender: TObject; E: Exception);
  private
    FDeviceID: Word;
    { Private declarations }
  public
    procedure OpenMedia;
    procedure RecordMedia;
    procedure StopMedia;
    procedure SaveMedia;
    procedure CloseMedia;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var
  MyError,Flags: Longint;

  procedure TForm1.OpenMedia;
  var
    MyOpenParms: TMCI_Open_Parms;
    MyPChar: PChar;
    TextLen: Longint;
  begin
    Flags:=mci_Wait or mci_Open_Element or mci_Open_Type;
  with MyOpenParms do
    begin
      dwCallback:=Handle; // TForm1.Handle
      lpstrDeviceType:=PChar('WaveAudio');
      lpstrElementName:=PChar('');
    end;
  MyError:=mciSendCommand(0, mci_Open, Flags,
  Longint(@MyOpenParms));
  if MyError = 0 then
    FDeviceID:=MyOpenParms.wDeviceID;
  end;

  procedure TForm1.RecordMedia;
  var
    MyRecordParms: TMCI_Record_Parms;
    TextLen: Longint;
  begin
    Flags:=mci_Notify;
    with MyRecordParms do
    begin
      dwCallback:=Handle;  // TForm1.Handle
      dwFrom:=0;
      dwTo:=10000;
    end;
    MyError:=mciSendCommand(FDeviceID, mci_Record, Flags,
    Longint(@MyRecordParms));
  end;

  procedure TForm1.StopMedia;
  var
    MyGenParms: TMCI_Generic_Parms;
  begin
  if FDeviceID <> 0 then
    begin
      Flags:=mci_Wait;
      MyGenParms.dwCallback:=Handle;  // TForm1.Handle
      MyError:=mciSendCommand(FDeviceID, mci_Stop, Flags,
      Longint(@MyGenParms));
    end;
  end;

  procedure TForm1.SaveMedia;
    type    // not implemented by Delphi
      PMCI_Save_Parms = ^TMCI_Save_Parms;
      TMCI_Save_Parms = record
      dwCallback: DWord;
      lpstrFileName: PAnsiChar;  // name of file to save
    end;
  var
    MySaveParms: TMCI_Save_Parms;
  begin
    if FDeviceID <> 0 then
    begin
        // save the file...
      Flags:=mci_Save_File or mci_Wait;
      with MySaveParms do
        begin
          dwCallback:=Handle;
          lpstrFileName:=PChar('c:\message.wav');
        end;
      MyError:=mciSendCommand(FDeviceID, mci_Save, Flags,
      Longint(@MySaveParms));
    end;
  end;

  procedure TForm1.CloseMedia;
  var
    MyGenParms: TMCI_Generic_Parms;
  begin
    if FDeviceID <> 0 then
    begin
      Flags:=0;
      MyGenParms.dwCallback:=Handle; // TForm1.Handle
      MyError:=mciSendCommand(FDeviceID, mci_Close, Flags,
      Longint(@MyGenParms));
      if MyError = 0 then
        FDeviceID:=0;
    end;
  end;

  procedure TForm1.Button1Click(Sender: TObject);
  begin
    OpenMedia;
    RecordMedia;
  end;

  procedure TForm1.Button2Click(Sender: TObject);
  begin
    StopMedia;
    SaveMedia;
    CloseMedia;
  end;

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    Application.OnException := AppException;
  end;

  procedure TForm1.AppException(Sender: TObject; E: Exception);
  begin
    CloseMedia;
  end;

end.


Regards Barry
0
 

Author Comment

by:jsexton
ID: 1391872
Thanks Barry!

I decided not to use your example code - I'm just going to create a "template" wave file (created in Sound Recorder - just 58 bytes) and copy it for each recording (MediaPlayer just appends the recording to the "empty" wave) - not pretty, but this is just a quick-n-dirty program anyway.  Thanks for your help.  I'm giving you the points since you took the time to help me out.  I'm sure I can use the example in the future.

Cheers!
0
 
LVL 17

Expert Comment

by:inthe
ID: 1391873
OK
  Cheers :o)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question