Python CoolPlay for OS X

Update – a new version of PyPlay is now on GitHub. It has pretty colours and gives you the out time and everything.

PyPlayCoolPlay is a very useful Windows program that plays audio files. So what? Lots of programs do that. CoolPlay is useful in radio (and theatre too I’d imagine) because it plays a file in a playlist – and then stops. It waits until you press play again before going on to the next track – important if you need to talk between items and play them in on cue.

I did some work to get CoolPlay running in OS X using WineBottler, but the resulting file is huge and it’s far from flawless – it does work, though.

I’d really like to write a native OS X or Linux version of CoolPlay, but that’s beyond my coding skills at the moment. I have been learning a fair bit of Python lately, however, and I’ve been mucking about with a Python script that will do something similar on a Mac.

It’s VERY early days, but I present a prototype PyPlay! It needs to be in the same directory as your audio files and needs a pre-existing M3U playlist file (as used by CoolPlay, VLC, iTunes and other audio players). It should play any audio file format that OS X can natively play, including M4A, MP3 and WAV.

It reads the M3U playlist into an array (list) and strips out metadata and leaves it with a list of audio files. You can then pick a file to play by number, press ctrl-c to stop it and type ‘q’ to quit the program. It doesn’t do ANY of the useful things CoolPlay does, like show you elapsed and remaining play time, durations, allow you to manipulate the playlist etc. But it does play any audio files, one at a time.

It only works on Macs because it uses afplay to play the audio – a Linux version would be possible using VLC or similar… I’ll work on that. And track duration info should be easy to display using afinfo – indeed, if you uncomment a couple of lines in the code you can get a heap of info displayed as you play…

verbose PyPlay

It’s also written in Python2 not Python3 as that’s what comes bundled with OS X and I want it to run without installing any extra software. And you’ll need to run it from the Terminal by saving it as a file called PyPlay.py and typing python PyPlay.py

Next thing I’d like to do is have an option to build and change playlists (perhaps make one automatically from any audio files found in the directory), or move up and down an existing playlist, and show file durations. I think displaying elapsed and remaining time may be too much of a challenge, but displaying an ‘out time’ (the clock time when the file will finish playing) shouldn’t be too hard.

# PyPlay radio playout script by Giles Booth @blogmywiki
# written in Python2 to run on MacOS X without installing Python3
# only works in OS X as uses afplay to play audio files
# requires an M3U playlist file called playlist.m3u
# and audio files in same directory

import os
playlist = ""

def showTracks():
    os.system('clear')
    print 'Welcome to PyPlay!'
    print 'Here are your tracks:'
    print '---------------------'
    for x in range(len(trackArray)):
        print x+1, trackArray[x]
    print 'Press ctrl+c to stop playing, q to quit'

def playTrack(track):
    song = trackArray[track-1]
    # uncomment next 2 lines if you want track info shown
    # infoString = "afinfo " + song
    # os.system(infoString)
    trackString = "afplay " + song
    os.system(trackString)

playlist = open('playlist.m3u')

trackArray = playlist.readlines()

# iterate over list in reverse order deleting unwanted items
for i in range(len(trackArray)-1,-1,-1):
    # strip out \n characters at end of line
    # and strip out empty lines or metadata
    if trackArray[i].startswith('\n') or trackArray[i].startswith('#'):
        trackArray.pop(i)
    temp = trackArray[i].strip()
    trackArray[i] = temp

while True:
    showTracks()
    trackNo = raw_input('\nWhich track would you like to play? ')
    if trackNo == 'q':
        break
    elif int(trackNo) <1 or int(trackNo) > len(trackArray):
        print 'Not a valid track number'
    else:
        playTrack(int(trackNo))
This entry was posted in MacOS X, radio and tagged , , , . Bookmark the permalink.

3 Responses to Python CoolPlay for OS X

  1. Jindra says:

    Cool stuff, success on accessing the file, but i get confuse when the playlist containt flac file. do you any solution how to play such flac format instead of mp3 file to play using “afplay”?
    Thanks in advance

Leave a Reply