[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] pygame.mixer.Sound doesn't throw file not found



Been using pygame for 5 years and just noticed that pygame.mixer.Sound
doesn't throw anything on file not found. I'm guessing either it
should or we should update the example .py's to test for it.  It took
quite awhile to debug this since you assume it would throw if it
couldn't load the sound from the file.

Heres chimp.py's load_sound which I copied a long time ago and use in
all my games. If the wav file is not there this function returns some
kind of silent sound object with no error.

def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join(data_dir, name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error:
        print ('Cannot load sound: %s' % fullname)
        raise SystemExit(str(geterror()))
    return sound