FMX.Mobile.VideoPlayback Sample
This sample demonstrates the use of the TMediaPlayer and the TMediaPlayerControl components.
Contents
Location
You can find the VideoPlayback sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and navigate to:
Object Pascal\Mobile Snippets\VideoPlayback
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The VideoPlayback sample uses the TMediaPlayer and the TMediaPlayerControl components to demonstrate how to playback an existing video clip on your iOS or Android device, including:
- Selecting different videos.
- Playing and stopping the video.
- Moving the video forward, backward, to the beginning, and to the end.
- Using a TRectangle as parent for the TMediaPlayerControl to hide and show the video as well as to move up and down the position of the video.
How to Use the Sample
- Navigate to the location given above and open:
- Delphi: VideoPlayerTest.dproj.
- Select your target platform (iOS or Android supported).
- Press
F9
or choose Run > Run. - Select one of the files to play from the file list.
- Use the Play, Stop, To The Start, 10 Sec Backward, 10 Sec Forward, and To The End buttons to interact with the video.
- Use the Moving Parent Up, Moving Parent Down, and Change Parents Visible buttons to interact with the TRectangle that contains the TMediaPlayerControl.
Files
File in Delphi | Contains |
---|---|
VideoPlayerTest.dproj |
The project itself. |
uMain.fmx |
The main form where the components are located. |
uMain.pas |
Used to define and implement the sample. |
MP4\Kayaking.mp4, MP4\Ocean.mp4 |
The video files to play in the sample. |
Implementation
The Video
- The sample uses the TMediaPlayer component to access the media files.
- TMediaPlayerControl is used to display the video media file.
- The TMediaPlayerControl.MediaPlayer property is used to link the media player control with the media player to access the video files.
- TMediaPlayer.Play is used to play the video.
- TMediaPlayer.Stop is used to stop the video.
- TMediaPlayer.CurrentTime is used to know, as well as to specify the current position of the video.
- TMediaPlayer.Duration is used to know the total duration time of the video.
The Rectangle Used as Parent
- TRectangle is the rectangle that serves as parent for the TMediaPlayerControl component.
- TRectangle.Position with TPosition type is used to get and update the X and Y positions of the rectangle.
- The TRectangle.Visible property is used to hide and show the rectangle when the Change Parent Visible button is clicked.
- Since the rectangle is the parent of the media player control, changing the position as well as hiding and showing the rectangle also moves, hides or shows the video.
Selecting the Video
- When a list box item is clicked, the sample sets the TMediaPlayer.FileName property to the file that corresponds with the item that was clicked; combining the path to the file, according to the platform, and the list box item text.
The Timer
- The TTimer is used to repeat the following actions after a given interval, that specifies the time interval between the TTimer.OnTimer events:
- Setting the maximum value of the tbProcess track bar according to the Duration of the video file.
- Updating the Value of the tbProcess track bar to match the CurrentTime of the video file.
On From Creation
When the form is created the sample:
- Gets the appropriate paths to access the video files:
- The
Platform
property of TOSVersion is used to know the platform the sample is running on. Depending on the platform, a different path to the video is used.
- The
- Fills the TListBox with the available video files.
- Sets the volume:
- The Value of the tbVolume track bar is adjusted to the TMediaPlayer.Volume.
- Later, when the tbVolume track bar changes, the Value of the track bar is used to set the TMediaPlayer.Volume.
Uses
- FMX.Media.TMediaPlayer
- FMX.Media.TMediaPlayer.Play
- FMX.Media.TMediaPlayer.Stop
- FMX.Media.TMediaPlayer.CurrentTime
- FMX.Media.TMediaPlayer.Duration
- FMX.Media.TMediaPlayer.FileName
- FMX.Media.TMediaPlayer.Volume
- FMX.StdCtrls.TTrackBar
- FMX.StdCtrls.TTrackBar.Max
- FMX.StdCtrls.TTrackBar.Value
- FMX.StdCtrls.TTrackBar.OnChange
- FMX.Forms.TForm.OnCreate
- System.SysUtils.TOSVersion
- System.SysUtils.TOSVersion.TPlatform
- System.IOUtils.TPath.Combine
- System.IOUtils.TPath.GetDocumentsPath