Using MCI for MP3 Playback
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
Loadingstd::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. Playingstd::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. PausingszCommand = "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. UnpausingszCommand = "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. Stoppingstd::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. Unloadingstd::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
Discuss this article in the forums
See Also: © 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
|