Here is a simple CD player/pauser written in Python:

# - Play or pause an audio CD
# Author: Daniel S. Meyer
# Version 0.1
# Usage: python
# (On Windows you can create a shortcut whose target is for example 
# C:\installs\python\python.exe c:\bin\ , 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'

cd0 = pygame.cdrom.CD(0)
if cd0.get_busy():
    # Currently playing: pause it

    # Save our state for later resume
    cdstate = open(get_state_file_name(),'w')
    cdstate.write(str(cd0.get_current()[0]) + '\n') # current track
    cdstate.write(str(cd0.get_current()[1])) # position within track
    # Not currently playing: try to resume from saved state
      cdstate = open(get_state_file_name(), 'r')
      curtrack = int(cdstate.readline())
      pos = float(cdstate.readline())
      # If anything goes wrong restoring our state,
      # just start playing at the beginning
      curtrack = 0
      pos = 0,pos,None)

    # Work around bug where only first track plays


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? ;)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.