RPG Music’N’SFX – Handling AudioFile in AudioPlayer

(Original post from 25. Dec. 2011)

I’ve had a fundamental, little problem on my route to a successful audioplayer. I wasn’t sure how to let my AudioFile class interact with my AudioPlayer class.

I use the pyaudiere library and as a small example this is how you would play an audiofile with pyaudiere:

# Start a device with which you can play more than one file
device = audiere.open_device()

# Load a sound file into memory, then play it looped:
l = d.open_file('/path/to/your/audiofile', True) # True for streaming

# Note, I always use True for streaming because when using
# an ogg file without streaming, a blue screen occurs on my PC.

The main problem was that, if I create an instance of AudioFile on initialisation of my AudioPlayer class, then I would have to provide a valid path to an audiofile which would not allow for having an empty track folder (No files in the folder => no valid paths => Error on initialisation).

However, if I choose to initiate an AudioFile instance outside AudioPlayer, I had no control over the instance from the AudioPlayer class which is rubbish, too.

As a consequence, I decided to create an attribute called

When pressing any of the controls, I would call the following method:

# Avoids NoneTypeError when calling NoneType.stop()
if self.track.__class__.__name__ == "AudioFile":

That way, I managed to avoid the invalid path problem at the beginning and also sidestep the control problem. I know, it isn’t the most elegant way of dealing with this, but it is good enough for now…


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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