RTL.MediaPlayer Sample

From RAD Studio Code Examples
Jump to: navigation, search

MediaPlayer is a sample application with a client-server architecture where:

  • A client application is a remote control that can find media players (server applications) reachable using app tethering and control them (play/pause and volume control).
  • A server application is a media player that you can control from the application itself or remotely using a remote control (client application).

The server application is a FireMonkey application, and the client application is available both as a VCL application and as a FireMonkey application.

The server application is based on the MediaPlayerHD sample application.

Location

You can find the MediaPlayer project at:

  • Start | Programs | Embarcadero RAD Studio Rio | Samples and then navigate to either of the following folders:
    • CPP\RTL\Tethering\MediaPlayer
    • Object Pascal\RTL\Tethering\MediaPlayer
  • Subversion Repository:
    • You can find Delphi and C++ code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.

Description

This sample applications folder provides two client projects (VCL and FireMonkey) and a server project. The projects are designed so that you can run any number of instances of each at the same time. Many remote controls (client applications) may connect to the same media player (server application), and any remote control may connect to multiple media players, as long as they are reachable using app tethering.

In the server application (MediaPlayerHD) you have the following controls:

  • Load… lets you load a file to play. See Audio-Video in FireMonkey for file support information.
  • Play pauses the playback, Pause resumes the playback.
  • Clear unloads the currently-loaded media file.
  • If Visible is checked, the applications shows the video area. If Visible is not checked, the video is not visible.
  • The volume bar lets you adjust the playback volume.
  • The top-right corner shows information about the dimensions (nxnpx) and the duration (ns) of the loaded media file.
MediaPlayer.png


In the client application (MediaPlayerCommand or MediaPlayerVCLCommand) you have the following controls:

  • You can click Find Players to fill the list on top with reachable media players (server applications).
  • On the media player selected on the top list:
    • Play pauses the playback, Pause resumes the playback.
    • The volume bar lets you adjust the playback volume.
MediaPlayerRemoteControl.png

How to Use the Sample

  1. Navigate to the location given above and open MediaPlayerHD\MediaPlayerHD.dproj or MediaPlayerHD\MediaPlayerHD.cbproj.
  2. On the Projects Window:
    1. Double click the server application entry MediaPlayerHD.
    2. Select Run > Run to run the server application.
    3. Double click one of the client application entries, MediaPlayerCommand or MediaPlayerVCLCommand.
    4. Select Run > Run to run the selected client application.
Note: You can run any number of instances of each project at the same time.

Files

Each application in the project group contains a single source file that contains the class for the main form of the application.

Classes

Each application defines a single class that implements the main form of the application.

Implementation

Server Application

  • The application defines the following event handlers:
    • OpenButtonClick:
      1. Changes the value of OpenDialog1.Filter to the return value of TMediaCodecManager.GetFilterString.
      2. Displays OpenDialog1. If you select a file:
        1. Enables PauseButton and ClearButton.
        2. Changes the value of MediaPlayer1.FileName to OpenDialog1.FileName.
        3. If MediaPlayer1 successfully loads the selected media file into its Media property:
          1. Fills Label1.Text with "XxYpx Zs", where:
            • X is the value of MediaPlayer1.Media.VideoSize.Truncate.X.
            • Y is the value of MediaPlayer1.Media.VideoSize.Truncate.Y.
            • Z is the value of MediaPlayer1.Media.Duration divided by MediaTimeScale to get the duration in seconds.
          2. Changes the value of TrackBar1.Max to MediaPlayer1.Media.Duration.
          3. Changes the value of VolumeTrack.Value to MediaPlayer1.Media.Volume.
          4. Starts playing the specified media file.
    • acPlayPauseExecute. If the State of MediaPlayer1 is Playing, acPlayPauseExecute changes the Text of the PauseButton to "Play" and stops the playback. If the media player is paused, acPlayPauseExecute changes the text of the PauseButton to "Pause" and resumes the playback.
    • acClearExecute clears the media player and disables PauseButton and ClearButton.
    • CheckBox1Change changes MediaPlayerControl1.Visible to the value of CheckBox1.IsChecked.
    • FMXAppProfileResourceReceived changes the value of VolumeTrack.Value to the value of the incoming remote resource.
    • Timer1Timer changes the value of TrackBar1.Value to MediaPlayer1.CurrentTime.
    • TrackBar1Change changes the value of MediaPlayer1.CurrentTime to TrackBar1.Value.
    • VolumeTrackChange changes the value of MediaPlayer1.Volume based on the value of VolumeTrack.Value.
  • The application defines the following nonvisual components:
  • The application defines the following controls:
    • OpenButton executes OpenButtonClick when clicked.
    • PauseButton executes acPlayPause when clicked.
    • ClearButton executes acClear when clicked.
    • CheckBox1 (Visible) executes CheckBox1Change when toggled.
    • VolumeTrack executes VolumeTrackChange when the user changes its value.
    • Label1.
    • TrackBar1 executes TrackBar1Change when the user changes its value.

FireMonkey Client Application

  • The application defines the following methods:
    • CheckMediaPlayers returns True if there is an item selected in lbPlayers. If there is no selected item, CheckMediaPlayers returns False and shows a message.
  • The application defines the following event handlers:
    • If CheckMediaPlayers returns True, Button1Click runs the acPlayPause action on the remote media player selected in lbPlayers.
    • Button2Click clears lbPlayers and calls CommandManager.DiscoverManagers. CommandManager.DiscoverManagers eventually triggers CommandManagerEndManagersDiscovery.
    • CommandManagerEndManagersDiscovery attempts to pair with any remote manager, and if successful triggers CommandManagerEndProfilesDiscovery.
    • CommandManagerEndProfilesDiscovery:
      1. Clears lbPlayers.
      2. If there is a discovered remote profile with "FMXMediaPlayer" as description, adds the profile to lbPlayers.
      3. If lbPlayers is not empty, selects the first remote profile in the the list and connects to it.
    • CommandManagerRequestManagerPassword provides the password of the remote server application: "1234".
    • lbPlayersItemClick connects to the remote profile selected in lbPlayers.
    • VolumeTrackChange sends VolumeTrack.Value to the remote profile selected in lbPlayers.
  • The application defines the following nonvisual components:
  • The application defines the following controls:
    • lbPlayers executes lbPlayersItemClick when you click one of its items.
    • Button2 (Find Players) executes Button2Click when clicked.
    • Button1 (Play/Pause) executes Button1Click when clicked.
    • VolumeTrack executes VolumeTrackChange when the user changes its value.

VCL Client Application

  • The application defines the following methods:
    • CheckMediaPlayers returns True if there is an item selected in lbPlayers. If there is no selected item, CheckMediaPlayers returns False and shows a message.
  • The application defines the following event handlers:
    • If CheckMediaPlayers returns True, Button1Click runs the acPlayPause action on the remote media player selected in lbPlayers.
    • Button2Click clears lbPlayers and calls CommandManager.DiscoverManagers. CommandManager.DiscoverManagers eventually triggers CommandManagerEndManagersDiscovery.
    • CommandManagerEndManagersDiscovery attempts to pair with any remote manager, and if successful triggers CommandManagerEndProfilesDiscovery.
    • CommandManagerEndProfilesDiscovery:
      1. Clears lbPlayers.
      2. If there is a discovered remote profile with "FMXMediaPlayer" as description, adds the profile to lbPlayers.
      3. If lbPlayers is not empty, selects the first remote profile in the the list and connects to it.
    • CommandManagerRequestManagerPassword provides the password of the remote server application: "1234".
    • VolumeTrackChange sends VolumeTrack.Value to the remote profile selected in lbPlayers.
  • The application defines the following nonvisual components:
  • The application defines the following controls:
    • lbPlayers.
    • Button2 (Find Players) executes Button2Click when clicked.
    • Button1 (Play/Pause) executes Button1Click when clicked.
    • VolumeTrack executes VolumeTrackChange when the user changes its value.

Uses

See Also