You have probably seen many different GUI programs that can play music on CDs. Well, here is a console-based recipe that can do the same thing. This is committed for archival to be run under Python 2.5 or later versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import pygame.cdrom as face
def main():
face.init()
count = face.get_count()
if count == 0:
raw_input('There is no cdrom drive.')
elif count == 1:
cmd(face.CD(0))
else:
num = which_CD(count)
if num != -1:
cmd(face.CD(num))
face.quit()
def which_CD(maximum):
print 'You have %s cdrom drives.' % maximum
while True:
try:
num = int(raw_input('What cdrom drive do you want to use? '))
except ValueError:
print 'Please try typing a number.'
except:
return -1
else:
if 0 < num <= maximum:
return num - 1
print 'The number is out of range.'
def cmd(disc):
disc.init()
total = disc.get_numtracks()
table = map(disc.get_track_audio, range(total))
if sum(table) == 0:
print 'This is not an audio CD.'
else:
index = {}
count = 0
for i, b in enumerate(table):
if b:
count += 1
index[count] = i
cmd_line(disc, index)
disc.stop()
disc.quit()
def cmd_line(disc, index):
while True:
prompt = get_prompt()
if prompt == 'nop':
pass
elif prompt == 'help':
print 'help: get this message'
print 'nop: does nothing'
print 'total: shows total tracks'
print 'play: plays the selected track'
print 'quit: leaves the commmand line'
elif prompt == 'total':
print 'There are %s tracks.' % len(index)
elif prompt == 'play':
track = get_track(len(index))
if track != -1:
disc.play(index[track])
else:
print 'Okay ...'
elif prompt == 'quit':
return
else:
print '"%s" cannot be understood.' % prompt
def get_prompt():
try:
return raw_input('>>> ').lower()
except:
return 'nop'
def get_track(maximum):
while True:
try:
num = int(raw_input('What track should be played? '))
except ValueError:
print 'Please try typing a number.'
except:
return -1
else:
if 0 < num <= maximum:
return num
print 'The number is out of range.'
if __name__ == '__main__':
main()
|