Media Player and Delphi


Sample code show's how Delphi can control the MS Media Player that comes with your standard Windows O/S packages or installers as well and as follows:


procedure TMediaPlayerForm.ControlButtonTypeRBClick(Sender : TObject);

begin

        { if the user selected the media player buttons, hide the scroll box and show the media player }

        if ControlButtonTypeRB.ItemIndex = 0 then

                begin

                 ScrollBox.visible := false;

                 MediaPlayer.visible := true;                

                end        

        else        { ItemIndex = 1 }

                begin

                 MediaPlayer.visible := false;

                 ScrollBox.visible := true;        

                end;

end;


The Media Player component can control devices that provide a Media Control Interface (MCI). It provides a set of VCR-like buttons for playing, pausing, recording, etc.,


Set the DeviceType property to determine what type of media is to be played. Media that use disk files have a FileName property.


{ wave audio radio button OnClick event handler }


procedure TMediaPlayerForm.WaveAudioRBClick(Sender : TObject);

begin

        { set up file selector for .Wav files - if a file is selected (user pressed OK)  then set up the media player }

        FileSelector.FileName := '';

        FileSelector.Filter := 'wave audio files*.wav';


        if FileSelector.Execute then

        begin

                { close current media player and assign new one }        

                 MediaPlayer.Close;

                 MediaPlayer.DeviceType := dtWaveAudio;

                 MediaPlayer.FileName := FileSelector.FileName;

                 MediaPlayer.Open;


                { setup status bar display and enable select button }

                 FileNamePanel.Caption := LowerCase(FileSelector.FileName);

                 SelectButton.enable := true;

                 if MediaPlayer.Error <> 0 then

                    MessageDlg(MediaPlayer.ErrorMessage, mtError, [mbOk],0);

        end;

end;


{ CD audio radio button OnClick event handler }


procedure TMediaPlayerForm.CDAudioRBClick(Sender : TObject);

begin


        { close current Media player file - CD audio does not use file name }

        MediaPlayer.Close;

        MediaPlayer.DeviceType := dtCDAudio;

        MediaPlayer.FileName := '';

        MediaPlayer.Open;        


        FileNamePanel.Caption := 'Audio CD';

        SelectButton.enable := true;

        if MediaPlayer.Error <> 0 then

           MessageDlg(MediaPlayer.ErrorMessage, mtError, [mbOk],0);

end;


{ play button (in scroll box) OnClick event handler }


procedure TMediaPlayerForm.PlayButtonClick(Sender : TObject);

begin

        { use try block to report error if PC can't play the selected item  }

        try

                if PlayTwiceCB.State = cbChecked then

                begin

                 MediaPlayer.Wait := true;

                 MediaPlayer.Play;

                 MediaPlayer.Wait := false;

                 MediaPlayer.Play;        

                end        

                else

                        MediaPlayer.Play;

        except

                MessageDlg(MediaPlayer.ErrorMessage, mtError, [mbOk], 0);

        end;

end;


TApplication components, which encapsulates your entire application, can provide set up control to your form or related.


{ Form OnCreate and Application OnHint event Handler }


procedure TMediaPlayerForm.FormCreate(Sender : TObject);

var

        WinDir : array[0..144] of char;

begin

        { tell the media player not to finish the selection before processing the next code step }

        MediaPlayer.Wait := false;

        { assign application OnHint event handler, hint color, and pause }

        Application.OnHint := DisplayHint;

        Application.HintColor := clAqua;

        Application.HintPause := 200;


        { get windows directory as initial dir in the file selector }

        GetWindowsDirectory(WinDir, 144);

        FileSelector.InitalDir := StrPas(WinDir);

end;


{ displays application hints in hint panel - application OnHint event handler }

procedure TMediaPlayerForm.DisplayHint(Sender : TObject);

begin

     HintPanel.Caption := Application.Hint;

end;