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

The Github repo for the sources is here:

https://github.com/jimlawless/cmdmp3


// 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
Advertisements

About Jim Lawless

I've been programming computers for about 36 years ... 30 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, Nuts and Volts, and others.
This entry was posted in Programming and tagged . Bookmark the permalink.

13 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.

  3. GOZARCK says:

    Thank you Jim, i was made it an installer in batch, but The only thing missing was the background music.Now my batch installer is complete.
    thanks again.

  4. Doe says:

    Hey Jim, thanks for writing this tool. I’ve just found that it fails to play mp3 files that have spaces in the filenames every time. I tried some ways like and , just doesn’t work. It shows me this:
    Error 259 when sending Open Walk Away.mp3 Type MPEGVideo Alias theMP3
    Error 263 when sending Play theMP3 Wait

    Do you know how to solve this?

    • Doe says:

      I tried some ways like
      cmdmp3 “Walk Away.mp3”
      and
      cmdmp3 “” “Walk Away.mp3”

    • Jim Lawless says:

      Is it possible that you’re on a volume that does not allow shortened names?

      In the same folder, what output do you see if you type:

      dir /x “walk away.mp3”

      ?

      I had used a method in the original code to try to use the shortened version of the filename. Another user said they had luck with using single quotes around the filename. I would have to embed this in a new version ( likely as an option after the filename. )

      • Doe says:

        Thanks for your response!
        The output is like this:(Excuse me for using a Chinese system, but I think you can understand what the text means)
        ———————————————–
        E:\m\ali>dir /x “walk away.mp3”
        驱动器 E 中的卷是 新加卷
        卷的序列号是 7AF3-FC38

        E:\m\ali 的目录

        2013/09/24 17:03 4,160,288 Walk Away.mp3
        1 个文件 4,160,288 字节
        0 个目录 36,405,497,856 可用字节
        ————————————————-

        I tried single quotes, but the problem remains. It seems like when I use quotes, cmdmp3 is able to recognize which file to play, yet playing is still unable.

      • Jim Lawless says:

        As I suspected, you’re on a volume that doesn’t allow shortened filenames. I’ll have to add an option to enclose the filename in quotes before passing it into the media interface.

      • Doe says:

        Thanks!

      • Jim Lawless says:

        It’ll be a while before I am able to do this. Could you please try the following?

        Consider the filename walk away.mp3

        When you run cmdmp3, please first enclose walk away.mp3 in single-quotes:

        ‘walk away.mp3’

        Then, enclose that entire phrase in double-quotes:

        ” ‘walk away.mp3’ ”

        So that your command reads:

        cmdmp3 ” ‘walk away.mp3’ ”

        (Please do not copy and paste out of this reply as I think the editor will alter some of the dquote characters. )

      • Doe says:

        After I tried this, the output is

        Cannot shorten filename ” ‘walk away.mp3’ “

      • Jim Lawless says:

        OK. I’ll try to make a fix, soon.

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