Pygame - Access CDROM



The pygame library has pygame.cdrom module that enables the program to manage playback from audio CDs and DVDs. We need to explicitly initialize this module for its use.

>>> import pygame
>>> pygame.cdrom.init()

The module defines all important CD class to represent the CDROM device. The constructor requires ID of CDROM drive available, starting with 0.

>>> obj=pygame.cdrom.CD(0)

The CDROM object has access to following useful functions to control the playback.

init() initialize a cdrom drive for use
quit() uninitialize a cdrom drive for use
play() start playing audio
stop() stop audio playback
pause() temporarily stop audio playback
resume() unpause audio playback
eject() eject or open the cdrom drive
get_busy() true if the drive is playing audio
get_paused() true if the drive is paused
get_empty() False if a cdrom is in the drive
get_numtracks() the number of tracks on the cdrom
get_track_audio() true if the cdrom track has audio data
get_track_start() start time of a cdrom track
get_track_length() length of a cdrom track

First, initialize the object.

>>> obj.init()

To find out how many tracks are present in the current CD −

>>> obj.get_numtracks()
8

To start playing the required track, give its number to play() function.

>>> obj.play(4)

To pause, resume and stop the playback, we can use relevant functions listed above.

pygame library
Advertisements