Link to home
Start Free TrialLog in
Avatar of aperlitz
aperlitz

asked on

cd-tray open or closed

how can i check in delphi 2.0 if the cd-tray is open or closed?
how can i check if an audio-cd is inserted or a data-cd? checking for playable tracks doesn't work.
Avatar of Matvey
Matvey

Check out this component:
http://www.delphipages.com/edit/count.cfm?comID=367&Link=uploads%2FSystem%5FAPI%2FCDEvnt14%2Ezip
(all thanks go to ZifNab!)

ZifNab also found this pice of source for me once:

 Q) How can I tell if a given CD-ROM Drive contains an Audio CD?

 A) You can use the Windows API function GetDriveType() to test
   if the drive is a CD-ROM drive then use the Windows API function

 GetVolumeInformation() to test if the VolumeName is 'Audio CD'.
 Example:

 function IsAudioCD(Drive : char) : bool;
  var
    DrivePath : string;
    MaximumComponentLength : DWORD;
    FileSystemFlags : DWORD;
    VolumeName : string;
   begin
    Result := false;
    DrivePath := Drive + ':\';
    if GetDriveType(PChar(DrivePath)) <> DRIVE_CDROM then exit;
    SetLength(VolumeName, 64);
    GetVolumeInformation(PChar(DrivePath), PChar(VolumeName),                                                  Length(VolumeName),                                                    nil,                                                    MaximumComponentLength,                                                    FileSystemFlags, nil, 0);

 if lStrCmp(PChar(VolumeName),'Audio CD') = 0 then result := true;
 end;

   function PlayAudioCD(Drive : char) : bool;
   var
     mp : TMediaPlayer;
   begin
     result := false;
     Application.ProcessMessages;
     if not IsAudioCD(Drive) then exit;
     mp := TMediaPlayer.Create(nil);
     mp.Visible := false;
     mp.Parent := Application.MainForm;
     mp.Shareable := true;
     mp.DeviceType := dtCDAudio;
     mp.FileName := Drive + ':';
     mp.Shareable := true;
     mp.Open;
     Application.ProcessMessages;
     mp.Play;
     Application.ProcessMessages;
     mp.Close;
     Application.ProcessMessages;
     mp.free;
     result := true;
   end;

   procedure TForm1.Button1Click(Sender: TObject);
   begin
     if not PlayAudioCD('D') then
       ShowMessage('Not an Audio CD');
   end;

   Joe
   --
   Joe C. Hecht
   joehecht@gte.net

ASKER CERTIFIED SOLUTION
Avatar of BoRiS
BoRiS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aperlitz

ASKER

the tip about checking for audio-cd's is perfect. works really great. but i need to know how i can check if the door is open or closed. i know, how to open and to close the door, but i want to let the user do this by clicking only one button. so the state of the door has to be checked when the user clicks the button. any suggestions about this? (i knew cdevents.pas before, but it didn't help me).
aperlitz

The code I sent you will do it exactly that it closes the cd door if it's open to close it just cahnge the close stuff to open...

If you still need help leave your mail address and I'll send a example to you...

Later
BoRiS
Well, Boris, both our comments weren't answeres. Why didn't you just leave the question open?