Originally published on: Sun, 02 Aug 2009 03:44:19 +0000
Please note! If you're having difficulties compiling the C source code presented below, please see my post: Compiling C from the Command Line with Pelles C
( See also http://www.mailsend-online.com/blog/play-mp3-files-with-python-on-windows.html ... a newer post that describes how to use the same technique presented below to play MP3's in Python scripts. )
I was thinking about writing a scriptable MP3 player today and realized that I really didn't know how to cause an MP3 to play on a recent version of Windows. I chose to create a command-line MP3 utility named cmdmp3.
I had used the sndPlaySound() API call to play WAV files, but I had assumed that playing an MP3 wouldn't be quite that easy. I found that I needed to use the Media Control Interface API via the mciSendString() function.
I'm no stranger to writing C code to use Windows API calls. I expected that I'd have to fill out a bunch of data structures with a DWORD in the front to determine the given structure's size and assumed that I'd have to learn a lot of new bitwise flags.
I was pleasantly surprised; the mciSendString() function accepts a text command string with parameters. Instead of having to learn a constant to represent a WAIT flag, I simply needed to append the word "Wait" to the end of the string that plays the MP3.
To play an MP3, cmdmp3 issues three MCI commands:
Close All
Open filename Type MPEGVideo Alias theMP3
Play theMP3 Wait
The first command closes active media that's playing. I assume that this is only within the same process, as I was able to launch two concurrent invocations of cmdmp3.exe and heard both MP3's playing simultaneously.
The next command was the Open command. Note that the type is specified as MPEGVideo even though audio only will be played. the Alias option causes the symbol theMP3 to act as a sort of handle variable for use with the Play command.
The filename specified in the open cannot contain spaces as the MCI string parser will assume that the latter parts of such a filename are parameters. In order to accommodate common file paths with spaces in them, cmdmp3 calls the GetShortPathName() API function to obtain an equivalent filename with all of the spaces removed.
The final command that causes the MP3 to be heard is the Play command. I added the "Wait" option at the end of the play string to prevent the API from returning while the MP3 plays. This would have caused the cmdmp3 process to terminate. Terminating cmdmp3 will halt the playing MP3.
After writing cmdmp3, I realized that others might enjoy using it, but might not want a console window to be displayed while the MP3 plays. You'll see an alternate version of the source called cmdmp3win that provides a window-only version of the player.
The program cmdmp3win.exe does not create its own window ... it simply tries to play the MP3, then terminates.
The command-line syntax is spartan; the first parameter of each program is used as the filename. Filenames with spaces in their name or path should be enclosed in double-quotes.
cmdmp3 "c:\my songs\something.mp3" or cmdmp3win "c:\my songs\something.mp3"
cmdmp3.c
cmdmp3win.c
The source and EXE files for cmdmp3 and cmdmp3win can be found here.
http://www.mailsend-online.com/wp/cmdmp3.zip
Unless otherwise noted, all code and text entries are Copyright ©2009 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.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Compiling C from the Command Line with Pelles C
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
COM Scripting in C by way of JavaScript
Switching a Console Window to Full Screen or Windowed Mode
Play MP3 Files with Python on Windows
Java in a Windows EXE with launch4j
A Simple Media Control Interface Script Processor
My First C64 Interrupt Program
Playing with OS/X Text to Speech
The Villain at the end of Marvel's Avengers Move is...
Processing GMail items with Ruby
Happy 30th Birthday, Commodore 64
MailWrench CSharp Command Line Mailer for Windows is now Free Software
Rainy Day Fun with the HTML DOM
Building a World War II Foxhole Radio
Prototerp – Unleashing a JavaScript REPL via the Mobile Browser