Title: CD-ROM Tutorial

Author: Scott L. Patterson (patterson at accesstoledo.com)
Submission date: May 18, 2001

Description: A short tutorial demonstrating most of the CD functionality.

Download: tut_cd.tar.gz

pygame version required: Any
SDL version required: Any
Python version required: Any

Comments: Here's a submission that runs through most of the commands that you'll need to use if you want to play cd music using pygame. Mr. Patterson's script provides some information about one of the present cdrom drives, gives us a track listing, then plays, pauses and resumes a music track. Finally, it attempts to eject the cd.

The code is pretty straightforward; I only found a couple of things confusing. First, the play/pause/resume loop is coded so that you expect it to play each track on the cd. However, it ejects the cd at the end of the loop, then breaks out of the for loop, meaning that only the first song is played. It's unclear from the code whether or not this was the intent. Second, I was unable to get the code to run without a pygame.error until I changed line 13 to read: cd_object = pygame.cdrom.CD(0). I believe this was because I have two cd-rom drives in my machine; I'm not sure exactly why this didn't work. Perhaps it was an error on my part.

Messages: 0


import pygame

def main():
    "Demo the CD functionality"

    # Initialize the CDROM device
    pygame.cdrom.init()
    print "CDROM initialized: ", pygame.cdrom.get_init()
    print "Number of CDROMs:  ", pygame.cdrom.get_count()

    # Create a CDROM object if possible
    if pygame.cdrom.get_count() > 0:
        cd_object = pygame.cdrom.CD(pygame.cdrom.get_count()-1)
        print "CDROM address:     ", cd_object

        # These two functions do NOT require the CD to be initialzed
        print "CDROM name:        ", cd_object.get_name()
        print "CDROM id:          ", cd_object.get_id()

        # the following functions require that the CD is initialized
        cd_object.init()

        # Print information about each track nicely formatted
        print
        print "Track  Audio  Start  Length"
        print "-----  -----  -----  ------"
        for i in range(cd_object.get_numtracks()):
            print "%5d  %5d  %5d  %6d" % (i, 
                  cd_object.get_track_audio(i),
                  cd_object.get_track_start(i),
                  cd_object.get_track_length(i))
        print

        # Let's play an audio track (hopefully)
        audio_track_found = 0
        for i in range(cd_object.get_numtracks()):
            if cd_object.get_track_audio(i):
                audio_track_found = 1
                print "Playing Track ", i, " for five seconds..."
                cd_object.play(i)
                pygame.time.delay(5000)
                print "Pausing Track ", i, " for five seconds..."
                cd_object.pause()
                pygame.time.delay(5000)
                print "Resuming Track ", i, " for five seconds..."
                cd_object.resume()
                pygame.time.delay(5000)
                print "Stopping Track ", i, " for five seconds..."
                cd_object.stop()
                print "Ejecting CD..."
                cd_object.eject()
                cd_object.quit()
                break

        if audio_track_found == 0:
            print "No audio tracks found."

    else:
        print "Please insert a CD into the CDROM and try again."

    # Clean up
    pygame.cdrom.quit()

if __name__ == '__main__': main()

Main - Repository - Submit - News

Feedback