Page 1 of 2

Length of an audio file (Wave Audio Pack)

PostPosted: May 1st, 2007, 4:14 pm
by Baxsan
How can i get the length of the currently playing (or opened) file?
Couldn't find any thread about this.

File is beeing played with the StockAudioPlayer

PostPosted: May 1st, 2007, 4:47 pm
by Kambiz
Look at the Length proprty of TWaveStreamAdapter class.

PostPosted: May 1st, 2007, 6:53 pm
by Baxsan
I'm new to this, so could you give me an example?
Like how can I use that class in the demo sound recorder?

PostPosted: May 1st, 2007, 7:15 pm
by Kambiz
While recording, use Position property of the recorder component to get current recording position.

When using TAudioPlayer or TAudioRecorder (while not recording), use the Wave.Length property.

For other cases use the following example as a template. The following function returns Length of a Wave file in milliseconds.

Code: Select all
uses WaveStorage;

function WaveLength(const FileName: String): DWORD;
var
  Stream: TStream;
  Adapter: TWaveStreamAdapter;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    Adapter := TWaveStreamAdapter.Create(Stream, soReference);
    try
      Result := Adapter.Length;
    finally
      Adapter.Free;
    end;
  finally
    Stream.Free;
  end;
end;

PostPosted: May 2nd, 2007, 4:52 pm
by Baxsan
Ok, thank you, I'll remember.

One non Wave Audio Package question:

What's the procedure that executes, when you close your application? I'd like to delete one file, before( or after) the program closes.

PostPosted: May 2nd, 2007, 6:27 pm
by Kambiz
Use OnClose event of the main form.

PostPosted: May 5th, 2007, 8:24 pm
by Baxsan
Edit: Actually I have another problem:


Code: Select all
function WaveLength(const FileName: String): DWORD;
var
  Stream: TStream;
  Adapter: TWaveStreamAdapter;
  filedir,todir:PAnsiChar;
  toname:string;
begin
  toname:=ExtractFilePath(Application.ExeName) + ExtractFileName('Templength.wav');
  todir:=pansichar(toname);
  filedir:=pansichar(filename);
  copyfile(filedir,todir,false);
  Stream := TFileStream.Create(toname, fmOpenRead);
  try
    Adapter := TWaveStreamAdapter.Create(Stream, soReference);
    try
      Result := Adapter.Length;
    finally
      Adapter.Free;
    end;
  finally
    Stream.Free;
  end;
  deletefile(toname);
end;


The function should copy the mainfile, name it "Templength" and to all the work with it and the delete it. The problem is that it says that it can't find the file, though it's right there.

It seems to have somekind of delay, when I remove the deleting part of the code and creat the "Templength.wav" manually, before the function executes. By the delay, I mean that it doesn't use the file that was just created (copied), but the previously created files.

PostPosted: May 5th, 2007, 11:29 pm
by Kambiz
Sorry, but I couldn't get what you are going to do. The logic behind is very confusing. :?

PostPosted: May 6th, 2007, 6:39 am
by Johnny_Bit
it looks like he's trying to do new blank wav file with lenght of known file. Am I right?

PostPosted: May 6th, 2007, 8:15 am
by Baxsan
That's exactly what I'm trying to do.


Edit: I made some changes and now it works properly.

But what about volume changing?

This doesn't seem to work?

Code: Select all
procedure TForm.Volumebar1Change(Sender: TObject);
begin
  Stockaudioplayer1.volume:=volumebar1.position;
end;

PostPosted: May 6th, 2007, 4:46 pm
by Kambiz
At designtime, set Options property to [woSetVolume].

If you use TAudioMixer, you'll have more flexibility to control volume.

PostPosted: May 7th, 2007, 4:02 pm
by Baxsan
Yes! It's working!

I'll propmise that this(these) will be the last question(s):

Is it true that when you pause the StockAudioPlayer, then the last position(before the pause) is stored somewhere and used, when its unpaused again?

If it's true, then is it possible to change it, while the player is paused?

PostPosted: May 7th, 2007, 9:24 pm
by Kambiz
Yes!

PostPosted: May 8th, 2007, 8:04 am
by Baxsan
How can I do that?

PostPosted: May 8th, 2007, 12:56 pm
by Kambiz
Using the Position property.
Please read the ReadMe file.