Using MCI for MP3 Playback

Published March 03, 2004 by Rob Zimmerman, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement

There are several ways to play back MP3s using free or commercial libraries. One of the simplest free methods I have stumbled across actually entails using the Windows MCI facility. The following code snippets show how easy it is to get your favorite MP3s streaming in no time. In case you aren't familiar with how to use MCI I'll give a brief overview for how to work with the MCI function calls provided by the Win32 SDK.

The MCI facility provides a mechanism for applications to work with video and sound devices. You can play AVIs, control a laserdisc player, control your CDROM's audio playback capability, and many other actions. All of the MCI capabilities can be accessed through the mciSendString() function call. The function basically takes a command, in string form, and then performs your request. For more information on what commands are available you should look up mciSendString in the MSDN. Ok, now let's get on to what commands we should pass mciSendString to get our music playing.

Note: In the following examples I'm using the Standard Template Library string. You can feel free to use good 'ol char *'s or CStrings if you prefer.

Required Header File: mmsystem.h
Required Library File: winmm.lib

Loading


std::string szCommand = "open \"" + szFileName + "\" type mpegvideo alias " + szFileName;
mciSendString(szCommand.c_str(), NULL, 0, 0);

//Parameter Description
'open'       - Inform MCI we'd like to load the resource in 'szFileName'
'szFileName' - This is the resource we'd like to open for playback.
'type mpegvideo' - Informing MCI what type of audio/video device we're working with.
'alias ***'  - The string we pass after alias is the character-based handle we're assigning to  
                this resource. Any future operations on this resource should use this name. 

Playing


std::string szCommand = "play " + szFileName + " from 0";
mciSendString(szCommand.c_str(), NULL, 0, 0);

//Parameter Description
'play'          - Obviously we'd like to have this resource start playing!
'szFileName'    - This is the character-based alias we gave when we issued the 'open' command.
'from *'        - The time we'd like to start playing this resource from. 
                  - You could easily use this for fast-forward/rewind functionality.

Pausing


szCommand = "pause " + szFileName;
mciSendString(szCommand.c_str(), NULL, 0, 0);           

//Parameter Description
'pause'         - Temporarily stop the audio playback.
'szFileName'    - The character-based name that was used when we called the 'open' command.

Unpausing


szCommand = "resume " + szFileName;
mciSendString(szCommand.c_str(), NULL, 0, 0);

//Parameter Description
'resume'        - Begin playback from previous call to 'pause'.
'szFileName'    - The character-based name that was used when we called the 'open' command.

Stopping


std::string szCommand = "stop " + szFileName;
mciSendString(szCommand.c_str(), NULL, 0, 0);

//Parameter Description
'stop'          - No surprise here, this stops the audio!
'szFileName'    - The character-based name that was used when we called the 'open' command.

Unloading


std::string szCommand = "close " + szFileName;
mciSendString(szCommand.c_str(), NULL, 0, 0);

//Parameter Description
'close'         - This command will release any resources that MCI is using for the resource.
'szFileName'    - The character-based name that was used when we called the 'open' command.

Almost there...

Just kidding....that's it! Those are all the commands you'll need to get started playing MP3's in your application. In case you're the C++ type, I've got a header file for you to get off and running with. Also included is a very simple console-based client that uses the included class.

For Questions/Comments please feel free to write Rob at rzimmerm@23rdhour.com
Visit us on the web at http://www.23rdhour.com

Cancel Save
0 Likes 0 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

Explains how to use Windows MCI to play MP3 files.

Advertisement
Advertisement
Advertisement