Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TMediaPlayer.TimeFormat

Status
Not open for further replies.

RowanHill

Programmer
Jan 6, 2002
4
GB
Hello, all,
I'm trying to display the time of an audio file in a lable, so I
Code:
type
  HMSRec = record
    Hours: byte;
    Minutes: byte;
    Seconds: byte;
    NotUsed: byte;
  end;

...

procedure TForm1.FormCreate(Sender: TObject); 
var
  TheLength: LongInt;
begin
  MediaPlayer1.FileName := 'H:\Blur - woohoo.mp3';
  MediaPlayer1.Open;
  MediaPlayer1.PLay;

  { Set time format - note that some devices don’t support tfHMS }
  MediaPlayer1.TimeFormat := tfHMS;
  { Store length of currently loaded media }
  TheLength := MediaPlayer1.TrackLength[1];
  with HMSRec(TheLength) do { Typecast TheLength as a HMSRec record }
  begin
    Label1.Caption := IntToStr(Hours); { Display Hours in Label1 }
    Label2.Caption := IntToStr(Minutes); { Display Minutes in Label2 }
    Label3.Caption := IntToStr(Seconds); { Display Seconds in Label3 }
  end;
  ShowMessage(IntToStr(THeLength));
end;


Much of this is from the help file, however, this then displays the song (which I know to be precisely 2 minutes long) as 58 hours, 251 minutes and 1 second long. What am I doing wrong?

Any help appreciated.
 
Sorry, it's OK, I've mulled it over a bit more, and although it doesn't seem to do what it says in the Delphi 6 help file, MediaPlayer1.Lengh does return miliseconds as default, and its easy enough to work it out from there.
 
procedure TfmPlayer.ReadMP3Info; // to display the length
var
Mins, Secs: Integer;
begin
TrackDoneGauge.MaxValue := MediaPlayer1.Length;
Mins := MediaPlayer1.Length Div 60000;
Secs := (MediaPlayer1.Length Mod 60000) Div 1000 ;
TrackLength.Text := Format('%.2d : %.2d', [Mins, Secs]);
end;

To get the current progress, in the timer event type:

CurTrkPos.Text := Format('%.2d : %.2d', [(MediaPlayer1.Position div 60000),
(MediaPlayer1.Position mod 60000 div 1000)]);
if TrackDoneGauge.MaxValue <> 0 then
TrackDoneGauge.Progress := MediaPlayer1.Position;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top