From Clomosy Docs

Revision as of 14:20, 24 December 2024 by ClomosyAdmin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

TclMediaPlayer is a component used for playing audio files. It allows you to perform basic media control functions such as playing, pausing, and stopping audio files.

Feature Use of Definition
TclMediaPlayer MediaPlayer1 : TclMediaPlayer A variable belonging to the TclMediaPlayer class is created.
FileName MediaPlayer1.FileName = clPathCombine('FonMusic1.mp3',Clomosy.AppFilesPath); Specifies the media file name played by the current media player. In the example, it takes the FonMusic1.mp3 file located in the project's directory.
Play MediaPlayer1.Play; It starts playing the media file. If the media file has been paused, the Play feature resumes the file from where it was paused. If the media file was previously stopped, the Play feature starts playing the file from the beginning.
Stop MediaPlayer1.Stop It stops playing the media file and rewinds it to the beginning. This means that the playback of the media file is completely terminated.
CurrentTime MediaPlayer1.CurrentTime = 0; Specifies the current playback position. CurrentTime is measured in 100ns. In the example, the time is set to take the audio file to the beginning.
Duration ShowMessage(MediaPlayer1.Duration) Specifies the total play time of the current media file attached to the media player. Duration is measured in 100ns.
State ShowMessage(MediaPlayer1.State) Specifies whether the current attached Media is playing or is stopped. If the current media file is not assigned or supported, then State is set to Unavailable. If the current media file is playing, then State is set to Playing, otherwise it is set to Stopped. (0:Unavailable, 1:Playing, 2:Stopped)
Volume MediaPlayer1.Volume = 0.3; Specifies the audio volume of the current media file. Volume takes values in the range from 0 through 1. If Volume is 1, then the media file is played at the maximum native volume. If Volume is 0, then the media file is mute. If there is no media file attached to the current media player, then Volume is 1.

Example 1
The example creates a user interface for playing and stopping a media file. First, a form is created from the TclForm class, and a TclMediaPlayer component is added to the form to load an audio file (uefa_League.wav). A panel (Panel1) is then created on the form, and two buttons (Start and Stop) are added to this panel. When the Start button is clicked, the media file is played from the beginning (resetting CurrentTime and calling Play); when the Stop button is clicked, the media file is stopped (calling Stop). The button events (tbeOnClick) are linked to a procedure called trigger, which checks which button was clicked and performs the appropriate media control action.

  var
   MyForm:TCLForm;
   MediaPlayer1 : TclMediaPlayer;
   Panel1 : TclPanel;
   Buton : TclButton;
   i : Integer;
   
  void trigger; 
  var
    clickedBtn:TClButton;
  {
    clickedBtn = TClButton(MyForm.Clsender); 
    if (clickedBtn.Hint == '0')
    {
      MediaPlayer1.CurrentTime = 0;
      MediaPlayer1.Play;
    }else
    {
       MediaPlayer1.Stop;
       MediaPlayer1.Volume = 0.3;
    }
  }
  
  {
    MyForm = TCLForm.Create(Self);
    
    MyForm.AddAssetFromUrl('https://clomosy.com/demos/uefa_League.wav');
    MediaPlayer1 = TclMediaPlayer.Create(MyForm);
    MediaPlayer1.FileName = clPathCombine('uefa_League.wav',Clomosy.AppFilesPath);
    
    Panel1 = MyForm.AddNewPanel(MyForm,'Panel1');
    Panel1.Align = alCenter;
    Panel1.Width = MyForm.clWidth / 2;
    Panel1.Height = MyForm.clHeight / 6;
    
    for (i = 0 to 1)
    {
      if(i == 0)
      {
        Buton = MyForm.AddNewButton(Panel1,'Buton'+IntToStr(i), 'Start');
        Buton.Align = AlLeft;
      }else
      {
        Buton = MyForm.AddNewButton(Panel1,'Buton'+IntToStr(i), 'Stop'); 
        Buton.Align = AlRight;
      }
      Buton.width = Panel1.width / 2;
      Buton.Hint = IntToStr(i);
      MyForm.AddNewEvent(Buton,tbeOnClick,'trigger');
    }
    MyForm.Run;
  }


Example 2
The application creates a game form (TclGameForm) and adds a button (TClButton) to this form. The button click event is handled by the Btn1Click procedure, where a sound file (soundFile) is played and the current media player (MediaPlayer1) is stopped. Additionally, a game timer (TClTimer) runs continuously, incrementing the label on the button every second (OnGameTimer procedure) and checking the status of the media player. If the media player has finished its duration or has been stopped, it is restarted. The program also downloads, saves, and plays specific sound and music files from given URLs.

  Var   
    MyForm:TclGameForm;
    Btn1 : TClButton;
    soundFile:Integer;
    MediaPlayer1:TclMediaPlayer;
    GameTimer: TClTimer;
  
  void Btn1Click;
  {
    myForm.PlayGameSound(soundFile);
    MediaPlayer1.Stop;
  }
  
  void OnGameTimer;
  {
    Btn1.ClTagInt = Btn1.ClTagInt + 1;
    Btn1.Text = IntToStr(Btn1.ClTagInt);
    
    //If the part time has run out or stopped, control to start again
    if ((MediaPlayer1.CurrentTime >= MediaPlayer1.Duration) || (MediaPlayer1.State == 2))
    {
      MediaPlayer1.CurrentTime = 0;
      MediaPlayer1.Play;
    }  
  }
  
  {
    MyForm = TclGameForm.Create(Self);
    
    Btn1= MyForm.AddNewButton(MyForm,'Btn1','Fire');
    MyForm.AddNewEvent(Btn1,tbeOnClick,'Btn1Click');
    Btn1.ClTagInt = 0;
    
    MyForm.AddAssetFromUrl('https://www.clomosy.com/game/assets/Fire.wav');
    MyForm.AddAssetFromUrl('https://www.clomosy.com/game/assets/FonMusic1.mp3');
    
    soundFile =  MyForm.RegisterSound('Fire.wav');
    MyForm.SoundIsActive=True;
    
    
    MediaPlayer1 = TclMediaPlayer.Create(MyForm);
    MediaPlayer1.FileName = clPathCombine('FonMusic1.mp3',Clomosy.AppFilesPath);
    MediaPlayer1.Play;
    
    //timer example : for continuous background music playing   
    GameTimer= MyForm.AddNewTimer(MyForm,'GameTimer',1000);
    MyForm.AddNewEvent(GameTimer,tbeOnTimer,'OnGameTimer');
    GameTimer.Enabled = True;
    
    MyForm.RunModal;
  }

See Also