Update to a Command Line MP3 Player for Windows

In 2009, I was tinkering with the Windows multimedia subsystem and I wrote a short program to play MP3 audio files from the command-line ( cmdmp3.exe ).  I created a second program that was a GUI application ( cmdmp3win.exe ) for those who didn’t want the console window to show.

Unfortunately, something about the old Microsoft C compiler that I use seems to trigger various anti-malware programs.

I adapted the two programs so that they would build under the MinGW GCC compiler this morning.

The sources and executable files for these programs can be found at:

http://www.mailsend-online.com/wp/cmdmp3new.zip

I will update the Github repo for the sources in a few days.  Here’s the source to the console version:


// cmdmp3
// A command-line MP3 player for Windows
// (console-mode version)
//
// License: MIT / X11
// Copyright (c) 2009, 2015 by James K. Lawless
// jimbo@radiks.net  http://jiml.us
// See http://www.mailsend-online.com/license2015.php
//
// To build, use the following MinGW command:
//    gcc -o cmdmp3.exe -lwinmm cmdmp3.c

#include <windows.h>
#include <stdio.h>
#include <string.h>

void sendCommand(char *);

int main(int argc,char **argv) {
   char shortBuffer[MAX_PATH];
   char cmdBuff[MAX_PATH + 64];
   printf("cmdmp3 v2.0\n");
   printf("Command-line MP3 player\n");
   printf("by Jim Lawless - http://jiml.us\n\n");
   
   if(argc<2) {
      fprintf(stderr,"Syntax:\n\tcmdmp3 \"c:\\path to file\\file.mp3\"\n");
      return 1;
   }
      // Get the shortened path because the MCI string interpreter uses spaces
      // as a separator.  If spaces appear in the commands, parts of the filename
      // would be interpreted as paramters to the given command.
   GetShortPathName(argv[1],shortBuffer,sizeof(shortBuffer));
   if(!*shortBuffer) {
      fprintf(stderr,"Cannot shorten filename \"%s\"\n",argv[1]);
      return 1;
   }   
   sendCommand("Close All");

   sprintf(cmdBuff,"Open %s Type MPEGVideo Alias theMP3",shortBuffer);
   sendCommand(cmdBuff);

   sendCommand("Play theMP3 Wait");
   return 0;
}

   // Send a string to the Media Control Interface
   // If an error occurs, display it and the string
   // that produced the error.
void sendCommand(char *s) {
   int i;
   i=mciSendString(s,NULL,0,0);
   if(i) {
         fprintf(stderr,"Error %d when sending %s\n",i,s);
   }
}

The command line to compile each is as follows:

gcc cmdmp3.c -lwinmm -o cmdmp3.exe

gcc cmdmp3win.c -lwinmm -mwindows -o cmdmp3win.exe

To play an MP3 file with either, enter:

cmdmp3 mp3filename

or

cmdmp3win mp3filename

About Jim Lawless

I've been programming computers for about 34 years ... 28 of that professionally. I've been a teacher, I've worked as a consultant, and have written articles here and there for publications like Dr. Dobbs Journal, The C/C++ Users Journal, and others.
This entry was posted in Programming and tagged . Bookmark the permalink.

3 Responses to Update to a Command Line MP3 Player for Windows

  1. Myron says:

    Thanks for writing this program. I just started using etmTk, which is a task manager program on Sourceforge, but it doesn’t have built-in functionality to actually play sound files for reminder alerts. Your program did the trick.

  2. ankur says:

    I renamed the executable to play.exe and now I tell my friends that my windows cmd is smarter than theirs as it plays files on typing “play filename.mp3”. Thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s