Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Error calculando duración MP3 (https://www.clubdelphi.com/foros/showthread.php?t=88942)

tarco35 30-08-2015 14:22:49

Error calculando duración MP3
 
Hola, tengo el siguiente codigo de delphi 6:
Código Delphi [-]

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:
Código Delphi [-]
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:
Código Delphi [-]
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:
Código Delphi [-]
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:

Código Delphi [-]
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 :)


La franja horaria es GMT +2. Ahora son las 18:47:38.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi