Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
Listing 9-11: Playing CD Audio tracks
    procedure TfrmCDExample.FormCreate(Sender: TObject);

    var

      OpenParms: TMCI_Open_Parms;

      StatusParms: TMCI_Status_Parms;

      SetParms: TMCI_Set_Parms;

      InfoParms: TMCI_Info_Parms;

      UniqueID: array[0..255] of char;

      NumTracks,

      iCount: Integer;

    begin

      {open the cd audio playback device}

      OpenParms.lpstrDeviceType := ‘cdaudio’;

      MCISendCommand(0, MCI_OPEN, MCI_OPEN_TYPE or MCI_WAIT, Longint(@OpenParms));

      {save a handle to the cd audio device}

      MCIDeviceID := OpenParms.wDeviceID;

      {determine if a CD is present in the drive}

      StatusParms.dwItem := MCI_STATUS_MEDIA_PRESENT ;

      MCISendCommand(MCIDeviceID, MCI_STATUS, MCI_WAIT or MCI_STATUS_ITEM,

                     LongInt(@StatusParms));

      if not LongBool(StatusParms.dwReturn) then

      begin

        ShowMessage(‘No CD is inserted in the CD-ROM drive. Please insert a CD’ +

                    ‘and try again.’);

        Application.Terminate;

      end;

      {retrieve and display the unique CD identification number. this is for

       information only, and is not required for playing a track from the CD}

      InfoParms.dwRetSize := 255;

      InfoParms.lpstrReturn := UniqueID;

      MCISendCommand(MCIDeviceID, MCI_INFO, MCI_INFO_MEDIA_IDENTITY or MCI_WAIT,

                     Longint(@InfoParms));

      lblUniqueID.Caption := UniqueID;

      {set the time format for the CD audio device}

      SetParms.dwTimeFormat := MCI_FORMAT_TMSF;

      MCISendCommand(MCIDeviceID, MCI_SET, MCI_WAIT or MCI_SET_TIME_FORMAT,

                     Longint(@SetParms));

      {retrieve the number of tracks on the CD}

      StatusParms.dwItem := MCI_STATUS_NUMBER_OF_TRACKS ;

      MCISendCommand(MCIDeviceID, MCI_STATUS, MCI_WAIT or MCI_STATUS_ITEM,

                     LongInt(@StatusParms));

      NumTracks := StatusParms.dwReturn;

      {for each track, indicate its length}

      for iCount := 1 to NumTracks do

      begin

        StatusParms.dwTrack := iCount;

        StatusParms.dwItem := MCI_STATUS_LENGTH;

        MCISendCommand(MCIDeviceID, MCI_STATUS, MCI_WAIT or MCI_STATUS_ITEM or

                       MCI_TRACK, LongInt(@StatusParms));

        {when polling for length on a specific track, the value returned in the TMSF

         format is not exactly what you would expect. the Track macro returns

         minutes, the Minute macro returns seconds, the Second macro returns frames,

         and the Frame macro returns nothing. notice also that we are saving the

         exact length of each track in the Objects property}

        lbxCDTracks.Items.AddObject(‘Track +IntToStr(iCount)+’ - +

                                    ‘Format(%d:%.2d’,

                                    [MCI_TMSF_TRACK(StatusParms.dwReturn),

                                    MCI_TMSF_MINUTE(StatusParms.dwReturn)]),

                                    TObject(MCI_MAKE_TMSF(

                                    iCount,MCI_TMSF_TRACK(StatusParms.dwReturn),

                                    MCI_TMSF_MINUTE(StatusParms.dwReturn),

                                    MCI_TMSF_SECOND(StatusParms.dwReturn))));

      end;

    end;

    procedure TfrmCDExample.lbxCDTracksDblClick(Sender: TObject);

    var

      PlayParms: TMCI_Play_Parms;

    begin

      {the user has indicated that another track should be played. therefore, stop

       any CD audio currently being output}

      MCISendCommand(MCIDeviceID, MCI_STOP, MCI_WAIT, 0);

      {the index of the selected item matches the index of the requested CD audio

       track. however, CD audio track indexes are one-based, and list box indexes

       are zero-based, so we must add a one to the selected item index. this

       indicates the starting track from which to begin playback}

      PlayParms.dwFrom := MCI_MAKE_TMSF(lbxCDTracks.ItemIndex+1, 0, 0, 0);

      {remember that we stored the actual length of each track in the Objects

       property along with the track name? since there is no track following the

       last track on the CD, we cannot tell the device to play the current track

       to the next track. to play the last track on the CD, we must indicate the

       exact length of that track. therefore, if we are playing the last track,

       we extract the track’s exact length from the Objects property. otherwise

       we can simply tell it to play to the next track}

      if lbxCDTracks.ItemIndex = lbxCDTracks.Items.Count-1 then

        PlayParms.dwTo := Longint(lbxCDTracks.Items.Objects[lbxCDTracks.ItemIndex])

      else

        PlayParms.dwTo := MCI_MAKE_TMSF(lbxCDTracks.ItemIndex+2, 0, 0, 0);

      {instruct the CD audio device to play the indicated selection}

      MCISendCommand(MCIDeviceID, MCI_PLAY, MCI_FROM or MCI_TO,

                     Longint(@PlayParms));

    end;

    procedure TfrmCDExample.TimerTimer(Sender: TObject);

    var

      StatusParms: TMCI_Status_Parms;

    begin

      {poll the CD to determine the position of the current playback}

      StatusParms.dwItem := MCI_STATUS_POSITION;

      MCISendCommand(MCIDeviceID, MCI_STATUS, MCI_WAIT or MCI_STATUS_ITEM,

                     LongInt(@StatusParms));

      {the returned position value is in TMSF format. thus, we must extract the

       appropriate minutes and seconds values}

      lblPosition.Caption := ‘Format(%d:%.2d’,

                             [MCI_TMSF_MINUTE(StatusParms.dwReturn),

                              MCI_TMSF_SECOND(StatusParms.dwReturn)]);

    end;

    procedure TfrmCDExample.FormClose(Sender: TObject; var Action: TCloseAction);

    begin

      {the application is terminating, so stop any CD audio playback, and

       close the CD audio device}

      MCISendCommand(MCIDeviceID, MCI_STOP, MCI_WAIT, 0);

      MCISendCommand(MCIDeviceID, MCI_CLOSE, 0, 0);

    end;
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.