Start New Discussion within our Software Development Community

Here is a short Python program using the module pygame that lets you play a midi music file. Midi files are instrumental music files that pack a lot of good sound into a small file. They are very popular with web page designers.

# play .mid music files using PyGame on your computer's sound card
# PyGame is free from:
# tested with Python25 and PyGame171      vegaseat     27aug2007

import pygame

def play_music(music_file):
    stream music with module in blocking manner
    this will stream the sound from disk while playing
    clock = pygame.time.Clock()
        print "Music file %s loaded!" % music_file
    except pygame.error:
        print "File %s not found! (%s)" % (music_file, pygame.get_error())
        # check if playback has finished

# pick a midi music file you have ...
# (if not in working folder use full path)
music_file = "Latin.mid"

freq = 44100    # audio CD quality
bitsize = -16   # unsigned 16 bit
channels = 2    # 1 is mono, 2 is stereo
buffer = 1024    # number of samples
pygame.mixer.init(freq, bitsize, channels, buffer)

# optional volume 0 to 1.0

except KeyboardInterrupt:
    # if user hits Ctrl/C then exit
    # (works only in console mode)
    raise SystemExit

I tried using this with pyscripter/Python 2.7/pygame 1.9.2 and it always said file not found. All the files were in the correct paths and i use professional MIDI files. Please help. if needed i can send you the file i used.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.