Jim Lawless' Blog


A Simple Media Control Interface Script Processor

Originally published on: Sat, 29 Aug 2009 12:18:17 +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

In my recent posts A Command Line CD Tray Opener and A Command Line MP3 Player for Windows, I wrote code to send simple command strings to the Media Control Interface via the mciSendString() API function.

I wanted to toy more with the Media Control Interface, so I wrote another short program, mciscript.c, which reads a text file and sends commands from the text file directly to the MCI. The purpose of mciscript is to provide a simple testbed for MCI commands, but it could be used as a practical utility for media control.

mciscript.c


// MCIScript
// Pass commands from a file to the Media Control Interface
//
// License: MIT / X11
// Copyright (c) 2009 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

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

#pragma comment(lib,"winmm.lib")
#pragma comment(lib,"kernel32.lib")

void sendCommand(char *);

int main(int argc,char **argv) {
   FILE *fp;
   char *p;
   
   char cmdBuff[1024];
   printf("MCIScript\n");
   printf("Send a batch of MCI commands to the Media Control Interface\n");
   printf("by Jim Lawless\n\n");
   
   if(argc<2) {
      fprintf(stderr,"Syntax:\n\tmciscript text-filename\n");
      return 1;
   }
   fp=fopen(argv[1],"r");
   if(fp==NULL) {
      fprintf(stderr,"Cannot open input file %s\n",argv[1]);
      return 1;
   }
   while(fgets(cmdBuff,sizeof(cmdBuff)-2,fp)!=NULL) {
         // # in the first position indicates a comment
      if(*cmdBuff=='#')
         continue;
         // see if we've got a blank line
      for(p=cmdBuff;*p;p++) {
         if( (*p!=' ')&&(*p!='\t')&&(*p!='\r')&&(*p!='\n'))
            break;
      }
      if(!(*p))
         continue;
            // chop off newline
      cmdBuff[strlen(cmdBuff)-1]=0;
      printf("%s\n",cmdBuff);
      sendCommand(cmdBuff);
   }
   fclose(fp);
   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 script format for mciscript is simple. If a '#' character is detected in the first position of a line, that line is ignored by the input processor. This allows mciscript to have a crude commenting ability.

Blank lines are also ignored by the input processor. Please note that execution of the script does not terminate if an error occurs on a particular line of input.

All other lines of text are sent to the Media Control Interface. Please consider the following file:

mci.txt


# play an MP3 file starting at 11 seconds
play some.mp3 from 11000 wait


# play an MPEG movie at fullscreen and repeat
# viewing when done.
play some.mpg fullscreen repeat wait

# open the CD tray
set CDAudio door open wait


# close the CD tray
set CDAudio door closed wait

One may invoke the above script by entering the command: mciscript mci.txt

The above script will cause some.mp3 to begin playing at 11 seconds ( 11000 milliseconds ). Then, it will play some.mpg and will repeat forever. Because of the inclusion of repeat and wait, the script will never reach the last two sections which open and close the CD tray door respectively.

At the time of this writing, a list of MCI command can be found here:

http://msdn.microsoft.com/en-us/library/ms710815(VS.85).aspx

The source and EXE files for mciscript can be found here.

http://www.mailsend-online.com/wp/mciscript.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.

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


Previous post: Scrolling GIF Banners with PerlMagick
Next post:Changing the C64 Text Color in C


About Jim ...


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

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Removing IE Popups in C

Screen Capture from Multiple Monitors in Java

A Command-Line CD Tray Opener

Book Review : Using Google App Engine

A Command Line Scheduler

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

A Simple ROT13 Macro

FIF Isn't Forth

Java in a Windows EXE with launch4j

Backlinks to my Blog Posts


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