PDA

Ver la Versión Completa : Error calculando duración MP3


tarco35
30-08-2015, 14:22:49
Hola, tengo el siguiente codigo de delphi 6:


type
HMSRec = record
Hours: byte;
Minutes: byte;
Seconds: byte;
NotUsed: byte;
end;

// y ya en código

If OpenDialog1.Execute then begin
Label16.Caption := OpenDialog1.FileName;
MediaPlayer1.FileName := OpenDialog1.FileName;
MediaPlayer1.Open;
MediaPlayer1.Rewind;
MediaPlayer1.TimeFormat := tfHMS;
TheLength := MediaPlayer1.Length;
with HMSRec(TheLength) do { Typecast TheLength as a HMSRec record }
begin
Label17.Caption := IntToStr(Hours);
Label17.Caption := Label17.Caption+'H ' + IntToStr(Minutes) + 'M ';
Label17.Caption := Label17.Caption+ IntToStr(Seconds)+ 'S ';
end;
end;


Y cuando selecciono un archivo MP3 el tiempo no lo hace bien, el fichero lo reproduzco con el VLC, por ejemplo y esta bien y la duracción que dice si es real, pero en este codigo, sacado de la ayuda, da unos valores muy diferentes.

Que estoy haciendo mal???
Gracias

ecfisa
30-08-2015, 22:36:50
Hola tarco35.

Intenta de este modo:

procedure TForm1.Button1Click(Sender: TObject);
var
len: Integer;
mp : TMediaPlayer;
begin
if OpenDialog1.Execute then
begin
mp := MediaPlayer1;
mp.FileName := OpenDialog1.FileName;
mp.TimeFormat := tfMilliseconds;
mp.Open;
len := Trunc(mp.Length / 1000);
Label16.Caption := mp.FileName;
Label17.Caption := Format('%d:%d:%d',[len div 3600, len div 60, len mod 60]);
mp.Close;
end;
end;


Saludos :)

tarco35
31-08-2015, 00:01:25
Va perfecto... muchas gracias.... un saludo.

ecfisa
31-08-2015, 00:25:35
Me alegro :)

Por si te resultase mas cómoda, rapida, etc., esta es otra forma en que podes conseguir la duración:

uses mmSystem;

function MP3Length(const FileName: string): string;
var
LenStr: string;
len : Integer;
begin
mciSendString(PChar(Format('open "%s" alias MP3',[FileName])), nil, 0, 0);
SetLength(LenStr, 255);
mciSendString('status MP3 length', PChar(LenStr), Length(LenStr), 0);
len := Trunc(StrToInt(LenStr) / 1000);
Result := Format('%d:%d:%d', [len div 3600, len div 60, len mod 60 ])
end;


Ejemplo de llamada:

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ShowMessage(MP3Length(OpenDialog1.FileName));
end;


Saludos :)

tarco35
31-08-2015, 00:39:09
Y ya ... hay alguna forma de saber cuando termina de reproducir un TmediaPlayer.. he probado con el evento OnNotify pero parace ser que solo se produce cuando haces click.

ecfisa
31-08-2015, 01:00:41
Hola de nuevo.

El evento es el correcto pero tendrías que evaluar la propiedad Mode, ejemplo:


procedure TForm1.MediaPlayer1Notify(Sender: TObject);
var
mp: TMediaPlayer;
begin
mp := TMediaPlayer(Sender);
if mp.Mode = mpStopped then
begin
ShowMessage('¡ Se terminó !');
mp.Notify := True;
end;
end;

( Recuerda que es una pregunta por hilo... ;) )

Saludos :)