MediaPlayer (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of the TMediaPlayer component, assuming that two buttons, for Open and Stop, are on the form. Note: AutoEnable must be set to False for you to be able to control the buttons manually.

Code

void __fastcall TForm2::btOpenClick(TObject *Sender)
{
	TOpenDialog* OpenMediaDialog;

	OpenMediaDialog = new TOpenDialog(this);
	OpenMediaDialog->Filter = "All Video Files (*.avi)|*.avi";
  // Browse for .avi files on your computer
	if (OpenMediaDialog->Execute())
	{
		/* Assign a file to the media player. */
		MediaPlayer1->FileName = OpenMediaDialog->FileName;

		/* Check if the file exists and is not a directory .*/
		if ((FileExists(OpenMediaDialog->FileName)) &&
		   (!DirectoryExists(OpenMediaDialog->FileName)))
		{
			/* Open the files. */
			MediaPlayer1->Wait = true;
			MediaPlayer1->Open();
			MediaPlayer1->Play();

			/* Override automatic button controlling. */
			MediaPlayer1->EnabledButtons =
				TButtonSet() <<
				TMPBtnType::btPause <<
				TMPBtnType::btStop <<
				TMPBtnType::btPlay;


			/* Enable the Stop button. */
			btStop->Enabled = true;
			btOpen->Enabled = false;
        }
	}

	delete OpenMediaDialog;
}

void __fastcall TForm2::btStopClick(TObject *Sender)
{
	/* Stop and close the media. */
	MediaPlayer1->Stop();
	MediaPlayer1->Close();

	MediaPlayer1->EnabledButtons = TButtonSet();

	/* Enable the Open button again. */
	btOpen->Enabled = true;
}

__fastcall TForm2::TForm2(TComponent* Owner)
  : TForm(Owner)
{
	/* Disable all the buttons.*/
	MediaPlayer1->AutoEnable = false;
	MediaPlayer1->EnabledButtons = TButtonSet();
}

void __fastcall TForm2::MediaPlayer1PostClick(TObject *Sender,
		  TMPBtnType Button)
{
	if (Button == TMPBtnType::btStop)
		btStop->Click();
}

Uses