Thema: Delphi Syntax SendMCICommand

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#38

AW: Syntax SendMCICommand

  Alt 25. Jul 2012, 17:43
Wäre es nicht geschickter den Alias beim Create in einer private Variable zu merken, dann muss der nicht bei jedem Aufruf wieder mitgegeben werden, sondern die Instanz verwaltet den selber?
Nein da der Alias von außen geändert werden muss.
Wenn zwei Instanzen Arbeiten benötigst du auch 2 Unterschiedliche alias was man machen könnte wäre ein
FAlias: array of string.
Nun ja, ich hatte an so etwas gedacht
Delphi-Quellcode:
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.
Aufruf mit:
Delphi-Quellcode:
var
  Player1, Player2 : TMciPlayer;
begin
  Player1 := TMciPlayer.Create( 'Player1', 'Sample1.wav' );
  Player2 := TMciPlayer.Create( 'Irgendwas2', 'Sample2.mp3' );

  Player1.Play;
  Player2.Play;

  // irgendwann dann auch mal

  Player1.Free;
  Player2.Free;
end;
Könnte man auch noch schön erweitern, so dass man auch das Soundfile der Instanz ändern kann.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat