How do you get the duration (in minutes and seconds) of an MP3/wav audio file in Delphi ?
It's been a long time since I played with Dephi.
Try,
FileSize(var aFile)
Mp3 are divided into frames like this
You will need to count the number of frames
Here is C# implementation that shouldn't be too difficult to translate. Look at the getLengthInSeconds function.
Not sure this will work, but I found this forum post. I'd compare results with something like winamp to make sure it works.
Go to www.un4seen.com and download bass library you will get a lot of info from the forum section. ;)
Under windows there is a reasonably effective way of determining the length of an MP3 file.
This is a huge hack but it seems to work.
Ryan.
//add MPlayer to the uses clause;
//
//add the MP3PlayLength function to an existing form and
//place a button on the form, linking the button click method to see how it works.
uses MPlayer;
function TForm1.MP3PlayLength(aMP3FileName:string):string;
var
wMP : TMediaPlayer;
wLen : Cardinal;
begin
Try
wMP := TMediaPlayer.Create(self);
try
wMP.Visible := false;
wMP.parent := self;
wMP.FileName := aMP3FileName;
wMP.TimeFormat := tfMilliseconds;
wMP.DeviceType := dtAutoSelect;
wMP.Open;
try
wLen := trunc(wMP.Length / 1000);
result := inttostr(wLen div 60)+':'+inttostr(wLen mod 60);
finally
wMP.Close;
end;
finally
wMP.free;
end;
except
result := '(err)';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
showmessage(MP3PlayLength(OpenDialog1.FileName));
end;
I recommend you to use BASS
http://www.un4seen.com/bass.html
BASS is an audio library .. to provide developers with powerful stream (MP3.. OGG.. ) functions. All in a tiny DLL, under 100KB in size.
it's very easy to use
uses BASS;
var
playingChannel: HSTREAM;
playingLength: Double;
mp3filename: String;
begin
BASS_Init(-1,44100,0,Application.Handle,nil);
playingChannel:=BASS_StreamCreateFile(FALSE,pchar(mp3filename),0,0,0);
playingLength:=BASS_ChannelBytes2Seconds(playingChannel,
BASS_ChannelGetLength(playingChannel,BASS_POS_BYTE));
end;
checkout this blog, using ffmpeg for background workaround in a ruby project.