Here is a simple CD player/pauser written in Python:
# Playcd.py - Play or pause an audio CD # Author: Daniel S. Meyer # Version 0.1 # # Usage: python playcd.py # (On Windows you can create a shortcut whose target is for example # C:\installs\python\python.exe c:\bin\playcd.py , and place it on # your Windows desktop. Then you can assign a shortcut key (say, # Ctrl+Alt+P) to that shortcut. Now you can play and pause your # CD by pressing the key combination.) # # TODOs: # - When saving the state, also save the list # of tracks (or at least the number of tracks) so # we can detect if the CD is changed and not "resume" # in the middle of a different CD. # - Add options for next/previous tracks # - Detect if a track is an audio track before attempting to play it # - Support playing a device other than the first one # import pygame,os def get_state_file_name(): return os.environ['TEMP']+'/playcd-state.tmp' pygame.cdrom.init() cd0 = pygame.cdrom.CD(0) cd0.init() if cd0.get_busy(): # Currently playing: pause it cd0.pause() # Save our state for later resume cdstate = open(get_state_file_name(),'w') cdstate.write(str(cd0.get_current()) + '\n') # current track cdstate.write(str(cd0.get_current())) # position within track cdstate.close() else: # Not currently playing: try to resume from saved state try: cdstate = open(get_state_file_name(), 'r') curtrack = int(cdstate.readline()) pos = float(cdstate.readline()) cdstate.close() os.remove(get_state_file_name()) except: # If anything goes wrong restoring our state, # just start playing at the beginning curtrack = 0 pos = 0 cd0.play(curtrack,pos,None) # Work around bug where only first track plays cd0.pause() cd0.resume() cd0.quit() pygame.cdrom.quit()
I set up the shortcut and shortcut key as described in the comments. Now I can play or pause an audio CD by pressing Ctrl+P.
(‘Course, I could have avoided this whole issue by getting a multimedia keyboard that has play and pause buttons on it… but then how would I learn Python? ;)