Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1077 → Rev 1078

/Delphi/akMediaAdmin/Source/UtilsUnit.pas
0,0 → 1,145
// akMediaAdmin v. 1.1
// Copyright (c) 1999-2000 Anatoli Klassen
 
unit UtilsUnit;
 
interface
 
uses MPlayer, MPlayerUnit;
 
function AddSlash(Path : string) : string;
function ShowTime(T : longint) : string;
function ExtrFileName(const FileName : string) : string;
 
type
// This class stores information about one item in the list:
// file name, title and duration
TFileInfo = class
private
FFileName : string;
FName : string;
FTime : longint;
FTimeString : string;
function GetLength : longint;
public
constructor Create(const AFileName, AName : string);
property FileName : string read FFileName;
property Name : string read FName;
property Time : longint read FTime;
property TimeString : string read FTimeString;
end;
 
implementation
 
uses SysUtils;
 
// Add slash to end of string if there is no one yet
function AddSlash(Path : string) : string;
begin
if Path = '' then Result := Path
else
if Path[Length(Path)] = '\' then Result := Path
else Result := Path + '\';
end;
 
// Convert duration from integer to string
function ShowTime(T : longint) : string;
var
a : longint;
 
begin
if T < 0 then
ShowTime := '00:00.0'
else begin
a := T div 1000 div 60; // minutes
Result := FormatFloat('00:', a);
T := T - a * 1000 * 60;
 
a := T div 1000; // seconds
Result := Result + FormatFloat('00"."', a);
T := T - a * 1000;
 
a := T div 100; // hundredths of seconds
Result := Result + FormatFloat('0', a);
end;
end;
 
// Extract file name without extention from full path
function ExtrFileName(const FileName : string) : string;
var
i : longint;
 
begin
Result := ExtractFileName(FileName);
i := Length(Result);
while (i > 0) and (Result[i] <> '.') do Dec(i);
if i > 0 then Result := Copy(Result, 1, i-1);
end;
 
// = TFileInfo =================================================================
constructor TFileInfo.Create(const AFileName, AName : string);
var
T : longint;
 
begin
FFileName := AFileName;
FName := AName;
 
if FName = '' then
FName := ExtrFileName(FFileName);
 
FTime := 0;
T := GetLength;
case T of
-1 : FTimeString := 'not found';
-2 : FTimeString := 'unknown type';
-3 : FTimeString := 'error';
else
FTime := T;
FTimeString := '<' + ShowTime(FTime) + '>';
end;
end;
 
// Returns:
// >= 0 - length
// -1 - file not found
// -2 - unknown format
// -3 - error
function TFileInfo.GetLength;
var
FPlayer : TPlayer;
 
begin
Result := -3;
 
if not FileExists(FFileName) then begin
Result := -1;
end
else
try
FPlayer := TPlayer.Create;
FPlayer.FileName := FFileName;
 
// find correct type of the file with 'cut and try' method
FPlayer.DeviceType := dtAutoSelect;
repeat
try
FPlayer.Open;
FPlayer.TimeFormat := tfMilliseconds;
Result := FPlayer.Length;
Break;
except
if FPlayer.DeviceType < dtWaveAudio then
FPlayer.DeviceType := Succ(FPlayer.DeviceType)
else begin
Result := -2;
end;
end;
until False;
 
FPlayer.Free;
except
end;
end;
 
end.