Site icon Learn C++

Learn To Develop Your Own Powerful Windows Media Player In C++ Builder

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.

  1. Create a new Multi-Device C++ Builder Project. Save all project files and unit in to a folder.
  2. 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
  3. Drag MediaPlayer from Tools Palette on to Form. This will allow you to play videos on your MediaPlayerControl.
  4. Drag MediaPlayerControl from Tools Palette. This is a frame that will show your videos. Set it’s Allign property to Client.
  5. 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

[crayon-661dfd12bfb80512659169/]

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.

[crayon-661dfd12bfb86748469277/]

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

[crayon-661dfd12bfb88307330266/]

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

[crayon-661dfd12bfb8a161373024/]

10. Go to Unit1.h and add this Media header under include lines as below if it is not added.

[crayon-661dfd12bfb8b444426739/]

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).

Exit mobile version