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 http://starship.python.net/crew/mhammond/win32/Downloads.html 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.

Edited 7 Years Ago by vegaseat: n/a

# Python supports COM, if you have the Win32 extensions
# check your Python folder eg.  D:\Python23\Lib\site-packages\win32com
# also http://starship.python.net/crew/mhammond/win32/Downloads.html
# 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")

mp.currentPlaylist.appendItem(tune)
mp.controls.play()

# to stop playing use
raw_input("Press Enter to stop playing")
mp.controls.stop()

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

Edited 7 Years Ago by vegaseat: n/a

Hi,

I have the win23com folder in my python directory.
C:\Python26\Lib\site-packages\win32com\client

I downloaded the win32com components from
http://starship.python.net/crew/mhammond/win32/Downloads.html

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.

Thanks,
Anil

Editor:
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.

Edited 7 Years Ago by vegaseat: n/a

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")

instead.

/ 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. ^.^

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

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

Edited 7 Years Ago by vegaseat: n/a

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: http://www.pyglet.org/download.html
# (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 = pyglet.resource.media(music_file)
music.play()

pyglet.app.run()

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

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

import pygame as pg

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


# 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
pg.mixer.music.set_volume(0.8)

play_music(music_file)

Edited 7 Years Ago by vegaseat: n/a

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: http://www.pyglet.org/download.html
# (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 = pyglet.resource.media(music_file)
music.play()
 
pyglet.app.run()

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\test.py", line 11, in <module>
    music = pyglet.resource.media(music_file)
  File "C:\Python25\lib\site-packages\pyglet\resource.py", 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?

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!

Edited 7 Years Ago by vegaseat: n/a

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