Ignore:
Timestamp:
Apr 23, 2012, 2:57:12 PM (13 years ago)
Author:
chronos
Message:
  • Modified: TPlayList is now registred as component in group CoolAudio in Component pallete.
  • Added: Created placeholder component TMediaPlayer which can be switched to different audio systems in runtime without destruction/construction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • CoolAudio/Systems/UAudioSystem.pas

    r352 r353  
    99
    1010type
     11  TMediaPlayerDriverClass = class of TMediaPlayerDriver;
    1112  TOutputDriver = (omAlsa, omOSS, omDirectX, omWin32);
    1213
     
    2223    constructor Create(AOwner: TComponent); override;
    2324    destructor Destroy; override;
     25    function GetMediaPlayerDriverClass: TMediaPlayerDriverClass; virtual;
    2426    property OutputMode: TOutputDriver read FOutputDriver write SetOutputMode;
    2527  end;
     
    2729  TAudioSystemClass = class of TAudioSystem;
    2830
    29   { TPlayer }
    30 
    31   TPlayer = class(TComponent)
     31  { TMediaPlayerDriver }
     32
     33  TMediaPlayerDriver = class
    3234  private
    3335  protected
     
    6062    property Playing: Boolean read FPlaying write SetPlaying;
    6163    property Active: Boolean read FActive write SetActive;
     64    constructor Create; virtual;
     65    destructor Destroy; override;
     66  end;
     67
     68  TMediaPlayer = class(TComponent)
     69  private
     70    procedure CheckDriver;
     71    function GetActive: Boolean;
     72    function GetAudioSystem: TAudioSystem;
     73    function GetFileName: string;
     74    function GetLength: TDateTime;
     75    function GetMuted: Boolean;
     76    function GetPlaying: Boolean;
     77    function GetPosition: TDateTime;
     78    function GetVolume: Real;
     79    procedure SetActive(AValue: Boolean);
     80    procedure SetAudioSystem(AValue: TAudioSystem);
     81    procedure SetFileName(AValue: string);
     82    procedure SetMuted(AValue: Boolean);
     83    procedure SetPlaying(AValue: Boolean);
     84    procedure SetPosition(AValue: TDateTime);
     85    procedure SetVolume(AValue: Real);
     86  public
     87    Driver: TMediaPlayerDriver;
     88    procedure Play;
     89    procedure Pause;
     90    procedure Stop;
     91    procedure Open;
     92    procedure Close;
     93    property Position: TDateTime read GetPosition write SetPosition;
     94    property Length: TDateTime read GetLength;
     95    property Playing: Boolean read GetPlaying write SetPlaying;
     96  published
     97    property Volume: Real read GetVolume write SetVolume; // 0..1
     98    property Muted: Boolean read GetMuted write SetMuted;
     99    property AudioSystem: TAudioSystem read GetAudioSystem write SetAudioSystem;
     100    property FileName: string read GetFileName write SetFileName;
     101    property Active: Boolean read GetActive write SetActive;
    62102    constructor Create(AOwner: TComponent); override;
    63103    destructor Destroy; override;
    64104  end;
    65105
    66   TPlayerClass = class of TPlayer;
     106  TPlayerClass = class of TMediaPlayerDriver;
    67107
    68108  { TAudioSystemManagerItem }
     
    71111    Name: string;
    72112    SystemClass: TAudioSystemClass;
    73     PlayerClass: TPlayerClass;
     113    Supported: Boolean;
    74114  end;
    75115
     
    78118  TAudioSystemManager = class(TComponent)
    79119    Systems: TObjectList; // TListObject<TAudioSystem>
    80     procedure Register(Name: string; SystemClass: TAudioSystemClass;
    81       PlayerClass: TPlayerClass);
     120    procedure Register(Name: string; SystemClass: TAudioSystemClass);
    82121    procedure FillStringList(StringList: TStrings);
    83     function SearchByName(Name: string): TAudioSystemManagerItem;
     122    function SearchByName(Name: string; SupportedOnly: Boolean = True): TAudioSystemManagerItem;
    84123    constructor Create(AOwner: TComponent); override;
    85124    destructor Destroy; override;
     
    90129  Mp3FileExt = '.mp3';
    91130
     131var
     132  DefaultAudioSystem: TAudioSystem;
     133
    92134resourcestring
    93135  SOpenOutputFailed = 'Failed opening audio output';
     136  SDefaultAudioSystemNotSet = 'Default audio system not set';
     137  SMediaPlayerDriverNotAssigned = 'Media player driver not assigned';
    94138
    95139
    96140implementation
     141
     142{ TMediaPlayer }
     143
     144function TMediaPlayer.GetLength: TDateTime;
     145begin
     146  CheckDriver;
     147  Result := Driver.Length;
     148end;
     149
     150procedure TMediaPlayer.CheckDriver;
     151begin
     152  if not Assigned(Driver) then
     153    raise Exception.Create(SMediaPlayerDriverNotAssigned);
     154end;
     155
     156function TMediaPlayer.GetActive: Boolean;
     157begin
     158  CheckDriver;
     159  Result := Driver.Active;
     160end;
     161
     162function TMediaPlayer.GetAudioSystem: TAudioSystem;
     163begin
     164  CheckDriver;
     165  Result := Driver.AudioSystem;
     166end;
     167
     168function TMediaPlayer.GetFileName: string;
     169begin
     170  CheckDriver;
     171  Result := Driver.FileName;
     172end;
     173
     174function TMediaPlayer.GetMuted: Boolean;
     175begin
     176  CheckDriver;
     177  Result := Driver.Muted;
     178end;
     179
     180function TMediaPlayer.GetPlaying: Boolean;
     181begin
     182  CheckDriver;
     183  Result := Driver.Playing;
     184end;
     185
     186function TMediaPlayer.GetPosition: TDateTime;
     187begin
     188  CheckDriver;
     189  Result := Driver.Position;
     190end;
     191
     192function TMediaPlayer.GetVolume: Real;
     193begin
     194  CheckDriver;
     195  Result := Driver.Volume;
     196end;
     197
     198procedure TMediaPlayer.SetActive(AValue: Boolean);
     199begin
     200  CheckDriver;
     201  Driver.Active := True;
     202end;
     203
     204procedure TMediaPlayer.SetAudioSystem(AValue: TAudioSystem);
     205var
     206  DriverClass: TMediaPlayerDriverClass;
     207begin
     208  FreeAndNil(Driver);
     209  DriverClass := AValue.GetMediaPlayerDriverClass;
     210  Driver := DriverClass.Create;
     211  Driver.AudioSystem := DefaultAudioSystem;
     212end;
     213
     214procedure TMediaPlayer.SetFileName(AValue: string);
     215begin
     216  CheckDriver;
     217  Driver.FileName := AValue;
     218end;
     219
     220procedure TMediaPlayer.SetMuted(AValue: Boolean);
     221begin
     222  CheckDriver;
     223  Driver.Muted := AValue;
     224end;
     225
     226procedure TMediaPlayer.SetPlaying(AValue: Boolean);
     227begin
     228  CheckDriver;
     229  Driver.Playing := AValue;
     230end;
     231
     232procedure TMediaPlayer.SetPosition(AValue: TDateTime);
     233begin
     234  CheckDriver;
     235  Driver.Position := AValue;
     236end;
     237
     238procedure TMediaPlayer.SetVolume(AValue: Real);
     239begin
     240  CheckDriver;
     241  Driver.Volume := AValue;
     242end;
     243
     244procedure TMediaPlayer.Play;
     245begin
     246  CheckDriver;
     247  Driver.Play;
     248end;
     249
     250procedure TMediaPlayer.Pause;
     251begin
     252  CheckDriver;
     253  Driver.Pause;
     254end;
     255
     256procedure TMediaPlayer.Stop;
     257begin
     258  CheckDriver;
     259  Driver.Stop;
     260end;
     261
     262procedure TMediaPlayer.Open;
     263begin
     264  CheckDriver;
     265  Driver.Open;
     266end;
     267
     268procedure TMediaPlayer.Close;
     269begin
     270  CheckDriver;
     271  Driver.Close;
     272end;
     273
     274constructor TMediaPlayer.Create(AOwner: TComponent);
     275begin
     276  inherited Create(AOwner);
     277  if not Assigned(DefaultAudioSystem) then
     278    raise Exception.Create(SDefaultAudioSystemNotSet);
     279  AudioSystem := DefaultAudioSystem;
     280end;
     281
     282destructor TMediaPlayer.Destroy;
     283begin
     284  FreeAndNil(Driver);
     285  inherited Destroy;
     286end;
    97287
    98288{ TAudioSystemManagerItem }
     
    111301end;
    112302
    113 function TAudioSystemManager.SearchByName(Name: string): TAudioSystemManagerItem;
     303function TAudioSystemManager.SearchByName(Name: string; SupportedOnly: Boolean = True):
     304  TAudioSystemManagerItem;
    114305var
    115306  I: Integer;
    116307begin
    117308  I := 0;
    118   while (I < Systems.Count) and (TAudioSystemManagerItem(Systems[I]).Name <> Name) do Inc(I);
     309  while (I < Systems.Count) and
     310    ((TAudioSystemManagerItem(Systems[I]).Name <> Name) or
     311    (not TAudioSystemManagerItem(Systems[I]).Supported and SupportedOnly)) do Inc(I);
    119312  if I < Systems.Count then Result := TAudioSystemManagerItem(Systems[I])
    120313    else Result := nil;
     
    122315
    123316procedure TAudioSystemManager.Register(Name: string;
    124   SystemClass: TAudioSystemClass; PlayerClass: TPlayerClass);
     317  SystemClass: TAudioSystemClass);
    125318var
    126319  NewItem: TAudioSystemManagerItem;
     
    129322  NewItem.Name := Name;
    130323  NewItem.SystemClass := SystemClass;
    131   NewItem.PlayerClass := PlayerClass;
    132324  Systems.Add(NewItem);
    133325end;
     
    145337end;
    146338
    147 { TPlayer }
    148 
    149 procedure TPlayer.SetActive(AValue: Boolean);
     339{ TMediaPlayerDriver }
     340
     341procedure TMediaPlayerDriver.SetActive(AValue: Boolean);
    150342begin
    151343  if FActive = AValue then Exit;
     
    153345end;
    154346
    155 procedure TPlayer.SetPlaying(AValue: Boolean);
     347procedure TMediaPlayerDriver.SetPlaying(AValue: Boolean);
    156348begin
    157349  if FPlaying = AValue then Exit;
     
    159351end;
    160352
    161 function TPlayer.GetMuted: Boolean;
     353function TMediaPlayerDriver.GetMuted: Boolean;
    162354begin
    163355  Result := False;
    164356end;
    165357
    166 procedure TPlayer.SetMuted(AValue: Boolean);
    167 begin
    168 end;
    169 
    170 function TPlayer.GetLength: TDateTime;
     358procedure TMediaPlayerDriver.SetMuted(AValue: Boolean);
     359begin
     360end;
     361
     362function TMediaPlayerDriver.GetLength: TDateTime;
    171363begin
    172364  Result := 0;
    173365end;
    174366
    175 function TPlayer.GetPosition: TDateTime;
     367function TMediaPlayerDriver.GetPosition: TDateTime;
    176368begin
    177369  Result := 0;
    178370end;
    179371
    180 function TPlayer.GetVolume: Real;
     372function TMediaPlayerDriver.GetVolume: Real;
    181373begin
    182374  Result := 0;
    183375end;
    184376
    185 procedure TPlayer.SetPosition(AValue: TDateTime);
    186 begin
    187 end;
    188 
    189 procedure TPlayer.SetVolume(AValue: Real);
    190 begin
    191 end;
    192 
    193 procedure TPlayer.SetFileName(AValue: string);
     377procedure TMediaPlayerDriver.SetPosition(AValue: TDateTime);
     378begin
     379end;
     380
     381procedure TMediaPlayerDriver.SetVolume(AValue: Real);
     382begin
     383end;
     384
     385procedure TMediaPlayerDriver.SetFileName(AValue: string);
    194386begin
    195387  if AValue = FFileName then Exit;
     
    199391end;
    200392
    201 procedure TPlayer.Play;
    202 begin
    203 end;
    204 
    205 procedure TPlayer.Pause;
    206 begin
    207 end;
    208 
    209 procedure TPlayer.Stop;
    210 begin
    211 end;
    212 
    213 procedure TPlayer.Open;
     393procedure TMediaPlayerDriver.Play;
     394begin
     395end;
     396
     397procedure TMediaPlayerDriver.Pause;
     398begin
     399end;
     400
     401procedure TMediaPlayerDriver.Stop;
     402begin
     403end;
     404
     405procedure TMediaPlayerDriver.Open;
    214406begin
    215407  Active := True;
    216408end;
    217409
    218 procedure TPlayer.Close;
     410procedure TMediaPlayerDriver.Close;
    219411begin
    220412  Active := False;
    221413end;
    222414
    223 constructor TPlayer.Create(AOwner: TComponent);
     415constructor TMediaPlayerDriver.Create;
    224416begin
    225417  inherited;
    226418end;
    227419
    228 destructor TPlayer.Destroy;
     420destructor TMediaPlayerDriver.Destroy;
    229421begin
    230422  Stop;
     
    257449end;
    258450
     451function TAudioSystem.GetMediaPlayerDriverClass: TMediaPlayerDriverClass;
     452begin
     453  Result := TMediaPlayerDriver;
     454end;
     455
     456initialization
     457
     458DefaultAudioSystem := TAudioSystem.Create(nil);
     459
     460finalization
     461
     462FreeAndNil(DefaultAudioSystem);
     463
    259464end.
    260465
Note: See TracChangeset for help on using the changeset viewer.