Media Players are good to play sounds or videos. You can easily create your own media player in C++ Builder. MediaPlayer (TMediaPlayer) component is very good to develop your media player application. You can use a TMediaPlayer component for easy access to media files.
MediaPlayer (TMediaPlayer) is used with TMediaPlayerControl. Set MediaPlayer to link a TMediaPlayer to this TMediaPlayerControl, so the video file attached to the media player is displayed on a form. The currently accessed media file is specified through the Media property. To specify the media file to be played by the current TMediaPlayer, set the FileName property. The file name must include the path of the file, so it can be localized on the memory, and the extension. Call the Play and Stop methods to start playing a media file, or to stop or pause a running media file.
Let’s create a new media player in C++ Builder.
- Create a new Multi-Device C++ Builder Project. Save all project files and unit in to a folder.
- Add a Layout, set it’s Align property to the Top. add four Buttons in to this Layout and change their Text properties to “Open“, “Play“, “Pause“, “Stop“
- Drag MediaPlayer from Tools Palette on to Form. This will allow you to play videos on your MediaPlayerControl.
- Drag MediaPlayerControl from Tools Palette. This is a frame that will show your videos. Set it’s Allign property to Client.
- Drag OpenDialog from Tools Palette, this dialog will be used open media files.
6. Double Click to Open button, this will create OnClick() event for this button and add this line
1 2 3 4 |
OpenDialog1->FileName=""; OpenDialog1->Execute(); |
this will delete previous filename if it has and it will execute OpenDialog, so user will able to browse his media file from its directory. If user choice a file, FileName property will have its path and file name together.
7. Now go back to Form design by clicking F12. Double click to Play button, this will create OnClick() event for this button, and add these lines as below.
1 2 3 4 5 6 7 |
if(FileExists(OpenDialog1->FileName) { MediaPlayer1->FileName=OpenDialog1->FileName; MediaPlayer1->Play(); } |
8. Go back to Form design, double click to Pause button, this will create OnClick() event for this button, and add these lines to Pause as below
1 2 3 |
MediaPlayer1->Pause(); |
9. Again go back to Form design and double click to Stop button, this will create OnClick() event for this button, and add these lines to Stop as below
1 2 3 |
MediaPlayer1->Stop(); |
10. Go to Unit1.h and add this Media header under include lines as below if it is not added.
1 2 3 |
#include <FMX.Media.hpp> |
11. All is done. As you see in C++ Builder, it is very easy to create your own Media Player. Now you can run your application by pressing F9.
You can use styled icons on your buttons or you can add icons or images on your buttons. You can use some effects on display and hide your layout which has buttons inside when mouse is used or not. You can create your own music player or video player with glamorous images or textures.
In MediaPlayer (TMediaPlayer), the current position is specified through the CurrentTime property. MediaPlayer also exposes media file properties such as Duration, VideoSize, Volume or State. You can also use more audio and video codecs by using MediaCodecManager (TMediaCodecManager).
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition