unit uMciPlayer;
interface
uses
Classes;
type
TMciPlayerState = ( mpsWait, mpsPlay, mpsPause );
TMciPlayer =
class
private
FAlias :
string;
FState : TMciPlayerState;
FDuration : Integer;
FLastResult : Cardinal;
FLastResultStr :
string;
function GetPosition : Integer;
protected
procedure DoCallCommand(
const CmdStr :
string );
public
constructor Create(
const aAlias, aFileName :
string; AutoPlay : Boolean = False );
destructor Destroy;
override;
procedure Play;
procedure Stop;
procedure Pause;
procedure Resume;
property Alias :
string read FAlias;
property State : TMciPlayerState
read FState;
property Duration : Integer
read FDuration;
property Position : Integer
read GetPosition;
property LastResult : Cardinal
read FLastResult;
property LastResultStr :
string read FLastResultStr;
end;
implementation
uses
Winapi.MMSystem, System.SysUtils;
{ TMciPlayer }
constructor TMciPlayer.Create(
const aAlias, aFileName :
string; AutoPlay : Boolean );
begin
inherited Create;
FState := mpsWait;
FAlias := aAlias;
FDuration := 0;
DoCallCommand( '
open "' + aFileName + '
" alias ' + aAlias );
if LastResult = 0
then
begin
DoCallCommand( '
set ' + FAlias + '
time format milliseconds wait' );
DoCallCommand( '
status ' + FAlias + '
length wait' );
FDuration := StrToIntDef( LastResultStr, 0 );
end;
if AutoPlay
then
Play;
end;
destructor TMciPlayer.Destroy;
begin
DoCallCommand( '
close ' + FAlias + '
wait' );
inherited;
end;
procedure TMciPlayer.DoCallCommand(
const CmdStr :
string );
var
lResultSize : Cardinal;
lReturn :
array [0 .. 255]
of WideChar;
begin
lResultSize := 255;
FLastResult := mciSendString( PWideChar( CmdStr ), lReturn, lResultSize, 0 );
if FLastResult <> 0
then
begin
mciGetErrorString( FLastResult, lReturn, 255 );
FLastResultStr := lReturn;
end
else
FLastResultStr := lReturn;
end;
function TMciPlayer.GetPosition : Integer;
begin
DoCallCommand( '
status ' + FAlias + '
position wait' );
Result := StrToIntDef( FLastResultStr, 0 );
end;
procedure TMciPlayer.Pause;
begin
if FState = mpsPlay
then
begin
DoCallCommand( '
pause ' + FAlias + '
notify' );
FState := mpsPause;
end;
end;
procedure TMciPlayer.Play;
begin
if FState = mpsWait
then
begin
DoCallCommand( '
play ' + FAlias + '
notify' );
FState := mpsPlay;
end
else
Resume;
end;
procedure TMciPlayer.Resume;
begin
if FState = mpsPause
then
begin
DoCallCommand( '
resume ' + FAlias + '
notify' );
FState := mpsPlay;
end;
end;
procedure TMciPlayer.Stop;
begin
if FState <> mpsWait
then
begin
DoCallCommand( '
stop ' + FAlias + '
notify' );
FState := mpsWait;
end;
end;
end.