CDROM Examples

Listing CD-ROM drives

    uses
SDL;

var
CDRom : string;
i : integer;

// Initialize SDL first
if ( SDL_Init( SDL_INIT_CDROM ) < 0 ) then
begin
MessageBox( 0, PChar( Format( 'Couldn''t initialize SDL : %s', [SDL_GetError] ) ), 'Error', MB_OK or MB_ICONHAND );
SDL_Quit;
halt(1);
end;

// Find out how many CD-ROM drives are connected to the system
CDRom := Format('Drives available: %d', [ SDL_CDNumDrives ] ) + #13#10;
for i := 0 to SDL_CDNumDrives - 1 do
begin
CDRom := CDRom + Format('Drive %d : "%s"',[ i, SDL_CDName( i ) ] ) + #13#10;
end;
MessageBox( 0, CDRom, 'Information', MB_OK or MB_ICONHAND );

Opening the default drive

  var    
cdrom : PSDL_CD;
status : TCDstatus;
status_str : string;

cdrom := SDL_CDOpen( 0 );
if ( cdrom = nil ) then
begin
MessageBox( 0, PChar( Format( 'Couldn''t open default CD-ROM drive : %s', [SDL_GetError] ) ), 'Error', MB_OK or MB_ICONHAND );
halt(2);
end;

status = SDL_CDStatus( cdrom );
case status of
CD_TRAYEMPTY:
begin
status_str := 'tray empty';
end;

CD_STOPPED:
begin
status_str := 'stopped';
end;

CD_PLAYING:
begin
status_str := 'playing';
end;

CD_PAUSED:
begin
status_str := 'paused';
end;

CD_ERROR:
begin
status_str := 'error state';
end;
end;

status_str := Format('Drive status : %s', status_str) + #13#10;
if ( status >= CD_PLAYING ) then
begin
int m, s, f;
FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
Format( 'Currently playing track %d, %d:%2.2d', [ cdrom.track[cdrom.cur_track].id, m, s ]);
end;

Listing the tracks on a CD

  var   
cdrom : PSDL_CD; // Assuming this has already been set..
i : integer;
m, s, f : integer;
status_str : string;
.
.
.
SDL_CDStatus( cdrom );
status_str := Format( 'Drive tracks: %d', [ cdrom.numtracks ] ) + #13#10;
for i := 0 to cdrom.numtracks - 1 do
begin
FRAMES_TO_MSF( cdrom.track[i].length, @m, @s, @f);
if ( f > 0 ) then
inc( s );
status_str := status_str + Format( '\tTrack (index %d) %d: %d:%2.2d', i, [ cdrom.track[i].id, m, s ]) + #13#10;
end;

Play an entire CD

  var    
cdrom : PSDL_CD; // Assuming this has already been set..
.
.
.
// Play entire CD:
if ( CD_INDRIVE( SDL_CDStatus( cdrom ) ) ) then
SDL_CDPlayTracks( cdrom, 0, 0, 0, 0 );

// Play last track:
if ( CD_INDRIVE( SDL_CDStatus( cdrom ) ) ) then
begin
SDL_CDPlayTracks( cdrom, cdrom.numtracks - 1 , 0, 0, 0 );
end;

// Play first and second track and 10 seconds of third track:
if ( CD_INDRIVE( SDL_CDStatus( cdrom ) ) ) then
SDL_CDPlayTracks( cdrom, 0, 0, 2, 10 );