当前位置:Linux教程 - Linux - 如何在LINUX下编写关于CD-ROM的程序(2)

如何在LINUX下编写关于CD-ROM的程序(2)



        
    -如何播放,暂停,停止CD

    --------------------------------------------------------------------------------

    译者:Bigluo

    有几个结构是来用描述CD音轨的确切位置的,他们是struct cdrom_tochdr和struct cdrom_ti,struct cdrom_msf。

    前者,当使用CDROMREADTOCHDR命令字的时候,返回第一道音轨和最后一道音轨的标号。后者,当使用CDROMPLAYTRKIND
    命令字的时候,可以用来播放一段音轨的特定范围,当然也可以用来在音轨的特定地方暂停或停止播放。第三个,当使用CDROMPLAYMSF命令字的时候,可以指定CD在X分X秒时的具体位置。

    struct cdrom_tochdr
    {
    u_char cdth_trk0; /* start track */
    u_char cdth_trk1; /* end track */
    };

    struct cdrom_ti
    {
    u_char cdti_trk0; /* start track */
    u_char cdti_ind0; /* start index */
    u_char cdti_trk1; /* end track */
    u_char cdti_ind1; /* end index */
    };

    struct cdrom_msf
    {
    u_char cdmsf_min0; /* start minute */
    u_char cdmsf_sec0; /* start second */
    u_char cdmsf_frame0; /* start frame */
    u_char cdmsf_min1; /* end minute */
    u_char cdmsf_sec1; /* end second */
    u_char cdmsf_frame1; /* end frame */
    };

    下面的程序将从头到尾的播放一张CD:

    #define CDDEVICE \"/dev/cdrom\"
    struct cdrom_tochdr header;
    struct cdrom_ti index;
    int cdrom;

    cdrom = open(CDDEVICE,O_RDONLY); // Open device
    ioctl(cdrom,CDROMREADTOCHDR,(void *) &header); // Get start and end tracks

    index.cdti_trk0=header.cdth_trk0; // Set first track
    index.cdti_ind0=0; // Start of track

    index.cdti_trk1=header.cdth_trk1; // Set final track
    index.cdti_ind1=255; // End of track

    iotcl(cdrom,CDROMPLAYTRKIND,(void *) &index); // Play the tracks
    Pausing, resuming, stopping and ejecting are done with ioctl calls using CDROMPAUSE, CDROMRESUME, CDROMSTOP and CDROMEJECT respectively:


    ioctl(cdrom,CDROMPAUSE,0); // Pause CD play.
    ioctl(cdrom,CDROMRESUME,0); // Resume CD play from where it was paused.
    ioctl(cdrom,CDROMSTOP,0); // Stop CD play (can\"t be RESUMEd)
    ioctl(cdrom,CDROMEJECT,0); // Eject CD tray.


    发布人:Bigluo 来自:Linux技术指南