Play MP3 files via Python's win32com support


Python can use its COM support and the OCX of the WindowsMediaPlayer to play mp3 music files. Make sure that your Python installation has the win32com folder. Check for the latest win32com support.

This code relies on file WMPlayer.OCX which MS has removed on newer versions of Windows. See the note on using modules pygame or pyglet in the comment posts.

About the Author


# Python supports COM, if you have the Win32 extensions
# check your Python folder eg.  D:\Python23\Lib\site-packages\win32com
# also
# this program will play MP3, WMA, MID, WAV files via the WindowsMediaPlayer

from win32com.client import Dispatch

mp = Dispatch("WMPlayer.OCX")

# use an mp3 file you have ...
#tune = mp.newMedia("C:/Program Files/Common Files/HP/Memories Disc/2.0/audio/Swing.mp3")

# or copy one to the working folder ...
#tune = mp.newMedia("Bier1.mp3")

# you can also play wma files, this cool sound came with XP ...
tune = mp.newMedia("C:/WINDOWS/system32/oobe/images/title.wma")


# to stop playing use
raw_input("Press Enter to stop playing")
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I think the file Swing.mp3 file came with my HP printer/scanner installation, and title.wma came with my Sony multimedia XP.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

To use COM in Python, you must install the Python Win32 extensions, downloaded the installer package from

am09 0 Newbie Poster


I have the win23com folder in my python directory.

I downloaded the win32com components from

But somehow I am not able to play the wma or mp3 files using the above example.

I tried it with Python 2.6 and Python 2.3 (the one used in your example) both and got the same result.

Am I missing something?

I am able to play the .wav file (using Python) but not the .wma and .mp3.

I will appreciate the help.


You may not have any of the music files given, or worse WMPlayer.OCX is missing. On my machine a Windows XP update remove it, when MS switched the Media Player.

NanatsuYoru 0 Newbie Poster

tune = mp.newMedia("C:/WINDOWS/system32/oobe/images/title.wma")

here is the error, use

tune = mp.newMedia("C:\WINDOWS\system32\oobe\images\title.wma")


/ is used by url
\ is used by system files.

another question, how to make a song not stop when end and keep it playing again, the repeat command, Thank for this wonderful code, it worked great with me after I made the fix. ^.^

xdmwe 0 Newbie Poster

Can you please send me the C:/Program Files/Common Files/HP/Memories Disc/2.0/audio/Swing.mp3 file, my e-mail is

Sorry, I switched to a Lexmark printer a few years ago and uninstalled the HP printer software.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

You should be able to use one of the following three folder syntaxes on Windows (at least Windows XP) ...

tune = mp.newMedia("C:/WINDOWS/system32/oobe/images/title.wma")

tune = mp.newMedia("C:\\WINDOWS\\system32\\oobe\\images\\title.wma")

tune = mp.newMedia(r"C:\WINDOWS\system32\oobe\images\title.wma")

My real problem is that one of the last XP updates removed WMPlayer.OCX so I can't use this program any longer. However, modules like pygame or pyglet handle MP3 sound. Here is an example ...

# play an MP3 music file using Python module pyglet
# download pyglet free from:
# (no GUI window/frame is created)

import pyglet

# pick an MP3 music file you have in the working folder
# otherwise use the full file path
# (also file names are case sensitive with pyglet)
music_file = "Boing.mp3"

music =

... and here is a pygame example ...

# play MP3 music files using Python module pygame
# pygame is free from:
# (does not create a GUI frame in this case)

import pygame as pg

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

# pick a MP3 music file you have in the working folder
# otherwise use the full file path
music_file = "Hot80s.mp3"

# set up the mixer
freq = 44100     # audio CD quality
bitsize = -16    # unsigned 16 bit
channels = 2     # 1 is mono, 2 is stereo
buffer = 2048    # number of samples (experiment to get right sound)
pg.mixer.init(freq, bitsize, channels, buffer)

# optional volume 0 to 1.0

masterofpuppets 19 Posting Whiz in Training

I've tried the pygame example and it works fine :) But for the pyglet one I have a problem. I downloaded and installed pyglet and when I run this:

# play an MP3 music file using Python module pyglet
# download pyglet free from:
# (no GUI window/frame is created)
import pyglet
# pick an MP3 music file you have in the working folder
# otherwise use the full file path
music_file = "Intro.mp3"
music =

I get the following error:

Traceback (most recent call last):
  File "C:\Documents and Settings\jori\My Documents\Gogo\University of Glasgow\First year ( Python )\Lectures\Computing Science\CS - 1P\Python\Games and stuff\Music Player\", line 11, in <module>
    music =
  File "C:\Python25\lib\site-packages\pyglet\", line 582, in media
    raise ResourceNotFoundException(name)
ResourceNotFoundException: Resource "Intro.mp3" was not found on the path.  Ensure that the filename has the correct captialisation.

the music file is in the working directory. Any ideas what might be going wrong?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I recall that pyglet is case sensitive to file names given, make sure you have that correct. Behaves a little bit like Unix even on a Windows machine. :)

Thanks for reminding me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.