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/Demo/UFormMain.pas

    r352 r353  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ComCtrls, ExtCtrls, ActnList, UAudioSystem, UAudioSystemFMOD,
    10   UAudioSystemMPlayer, UCoolAudio, UPlaylist;
     9  ComCtrls, ExtCtrls, ActnList, UAudioSystem,
     10  UCoolAudio, UPlaylist;
    1111
    1212type
     
    3232    ComboBoxBackend: TComboBox;
    3333    Label1: TLabel;
     34    Label2: TLabel;
    3435    LabelPosition: TLabel;
     36    MediaPlayer: TMediaPlayer;
    3537    OpenDialog1: TOpenDialog;
     38    PlayList: TPlayList;
    3639    TimerPlayback: TTimer;
    37     TrackBar1: TTrackBar;
     40    TrackBarPosition: TTrackBar;
     41    TrackBarVolume: TTrackBar;
    3842    procedure AOpenExecute(Sender: TObject);
    3943    procedure APauseExecute(Sender: TObject);
     
    4650    procedure FormCreate(Sender: TObject);
    4751    procedure FormDestroy(Sender: TObject);
     52    procedure FormShow(Sender: TObject);
    4853    procedure TimerPlaybackTimer(Sender: TObject);
    49     procedure TrackBar1Change(Sender: TObject);
     54    procedure TrackBarPositionChange(Sender: TObject);
     55    procedure TrackBarVolumeChange(Sender: TObject);
    5056  private
    51     { private declarations }
    5257  public
    53     AudioSystem: TAudioSystem;
    54     Player: TPlayer;
    55     Playlist: TPlaylist;
    5658    procedure UpdateInterface;
    5759  end;
     
    7476procedure TFormMain.FormCreate(Sender: TObject);
    7577begin
    76   Playlist := TPlaylist.Create(nil);
     78  PlayList := TPlayList.Create(nil);
    7779  AudioSystemManager.FillStringList(ComboBoxBackend.Items);
    7880  if ComboBoxBackend.Items.Count > 0 then
     
    8385procedure TFormMain.FormDestroy(Sender: TObject);
    8486begin
    85   FreeAndNil(Player);
    86   FreeAndNil(AudioSystem);
    87   FreeAndNil(Playlist);
     87  FreeAndNil(PlayList);
     88end;
     89
     90procedure TFormMain.FormShow(Sender: TObject);
     91begin
     92  UpdateInterface;
    8893end;
    8994
    9095procedure TFormMain.TimerPlaybackTimer(Sender: TObject);
    9196begin
    92   if Assigned(Player) and Player.Playing then begin
    93     TrackBar1.OnChange := nil;
    94     TrackBar1.Position := Trunc(Player.Position / Player.Length * TrackBar1.Max);
     97  if Assigned(MediaPlayer) and MediaPlayer.Playing then begin
     98    TrackBarPosition.OnChange := nil;
     99    TrackBarPosition.Position := Trunc(MediaPlayer.Position / MediaPlayer.Length * TrackBarPosition.Max);
     100    TrackBarVolume.OnChange := nil;
     101    TrackBarVolume.Position := Trunc(MediaPlayer.Volume * TrackBarVolume.Max);
    95102    Application.ProcessMessages;
    96     TrackBar1.OnChange := TrackBar1Change;
    97     LabelPosition.Caption := 'Position: ' + TimeToStr(Player.Position) + ' / ' + TimeToStr(Player.Length);
     103    TrackBarPosition.OnChange := TrackBarPositionChange();
     104    TrackBarVolume.OnChange := TrackBarVolumeChange();
     105    LabelPosition.Caption := 'Position: ' + TimeToStr(MediaPlayer.Position) + ' / ' + TimeToStr(MediaPlayer.Length);
    98106  end;
    99107end;
    100108
    101 procedure TFormMain.TrackBar1Change(Sender: TObject);
     109procedure TFormMain.TrackBarPositionChange(Sender: TObject);
    102110begin
    103   Player.Position := TrackBar1.Position / TrackBar1.Max * Player.Length;
     111  MediaPlayer.Position := TrackBarPosition.Position / TrackBarPosition.Max * MediaPlayer.Length;
     112end;
     113
     114procedure TFormMain.TrackBarVolumeChange(Sender: TObject);
     115begin
     116  MediaPlayer.Volume := TrackBarVolume.Position / TrackBarVolume.Max;
    104117end;
    105118
    106119procedure TFormMain.UpdateInterface;
    107120begin
     121  APlay.Enabled := not MediaPlayer.Playing;
     122  APause.Enabled := MediaPlayer.Playing;
     123  AStop.Enabled := MediaPlayer.Playing;
    108124  Caption := ApplicationTitle;
    109   if Assigned(Player) then Caption := Player.FileName + ' - ' + Caption;
     125  if Assigned(MediaPlayer) then Caption := MediaPlayer.FileName + ' - ' + Caption;
    110126end;
    111127
    112128procedure TFormMain.ComboBoxBackendChange(Sender: TObject);
    113129begin
    114   FreeAndNil(Player);
    115   FreeAndNil(AudioSystem);
    116130  if ComboBoxBackend.ItemIndex <> - 1 then begin
    117131    with TAudioSystemManagerItem(ComboBoxBackend.Items.Objects[ComboBoxBackend.ItemIndex]) do begin
    118       AudioSystem := SystemClass.Create(nil);
    119       Player := PlayerClass.Create(nil);
    120       Player.AudioSystem := AudioSystem;
    121       Playlist.Player := Player;
    122       //Player.Active := True;
     132      DefaultAudioSystem := SystemClass.Create(nil);
     133      MediaPlayer.AudioSystem := DefaultAudioSystem;
     134      PlayList.Player := MediaPlayer;
     135      //MediaPlayer.Active := True;
    123136    end;
    124137  end;
     
    127140procedure TFormMain.APlayExecute(Sender: TObject);
    128141begin
    129   Player.Play;
     142  MediaPlayer.Play;
     143  UpdateInterface;
    130144end;
    131145
    132146procedure TFormMain.APlayNextExecute(Sender: TObject);
    133147begin
    134   Playlist.PlayNext;
     148  PlayList.PlayNext;
    135149  FormPlaylist.ReloadList;
    136150end;
     
    138152procedure TFormMain.APlayPreviousExecute(Sender: TObject);
    139153begin
    140   Playlist.PlayPrevious;
     154  PlayList.PlayPrevious;
    141155end;
    142156
     
    148162procedure TFormMain.AStopExecute(Sender: TObject);
    149163begin
    150   Player.Stop;
     164  MediaPlayer.Stop;
     165  UpdateInterface;
    151166end;
    152167
    153168procedure TFormMain.APauseExecute(Sender: TObject);
    154169begin
    155   Player.Pause;
     170  MediaPlayer.Pause;
     171  UpdateInterface;
    156172end;
    157173
     
    159175begin
    160176  if OpenDialog1.Execute then begin
    161     Player.FileName := OpenDialog1.FileName;
    162     Playlist.AddFile(OpenDialog1.FileName);
    163     Player.Play;
     177    MediaPlayer.FileName := OpenDialog1.FileName;
     178    PlayList.AddFile(OpenDialog1.FileName);
     179    MediaPlayer.Play;
     180    UpdateInterface;
    164181  end;
    165182end;
Note: See TracChangeset for help on using the changeset viewer.