Jim Lawless' Blog


Play MP3 Files with Python on Windows

Originally published on: Sun, 26 Jun 2011.

I've just read the book Grey Hat Python : Python Programming for Hackers and Reverse Engineers by Justin Seitz. The book introduced the Python ctypes library to me. The ctypes library, allows a Python program to access to lower-level features of the operating environment normally reserved for C programmers.

( The book itself was a great read that deserves its own post ... after I've had time to tinker with all of the code. )

As an early experiment, I rewrote in Python the bulk of the command-line MP3 player I had originall written in C in this blog post: http://www.mailsend-online.com/blog/a-command-line-mp3-player-for-windows.html ...

Please refer to the script below:

playmp3.py

# Copyright (c) 2011 by James K. Lawless
# jimbo@radiks.net http://www.mailsend-online.com
# License: MIT / X11
# See: http://www.mailsend-online.com/license.php
# for full license details.

from ctypes import *;

winmm = windll.winmm

def mciSend(s):
   i=winmm.mciSendStringA(s,0,0,0)
   if i<>0:
      print "Error %d in mciSendString %s" % ( i, s )

def playMP3(mp3Name):
   mciSend("Close All")
   mciSend("Open \"%s\" Type MPEGVideo Alias theMP3" % mp3Name)
   mciSend("Play theMP3 Wait")
   mciSend("Close theMP3")

playMP3("test.mp3")

The ZIP archive containing the above source can be downloaded from http://www.mailsend-online.com/wp/playmp3.zip

Note that the above script does not play a script directly from the command-line; it exposes the function playMP3() which should allow one to write Python code to control the invocation of the given MP3 file through the Media Control Interface (MCI).

The script expects a file named test.mp3 to be present in the current directory. You can alter the last line of the script, substituting the name and location of a preferred MP3 file.

Please note that in the Python version of the code, I have omitted the call to the Win32 API function GetShortPathName(). Instead, I placed double-quotes around the name of the MP3 file in the MCI command-string to accommodate the occurrences of spaces in the filename and/or pathname.

Unless otherwise noted, all text in these examples is Copyright ©2011 by James K. Lawless.



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: My Journey Began with BASIC
Next post:Backlinks to my Blog Posts


About Jim ...


Subscribe to this blog!



Subscribe via e-mail


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A Forthcoming Marvel Movie Villain

Uninstalling Problematic Windows Software

Don't be Hatin'

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires


Random Posts

Open Source Licenses

Computers I Have Known

An SMTP Server Simulator in Perl

Switching a Console Window to Full Screen or Windowed Mode

Book Review : Using Google App Engine

The Protection Racket

Compiling Rhino JavaScript to Java

Scott Ballantyne: Blazin' Into Forth

Twimmando No More

Scrolling GIF Banners with PerlMagick


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service