Play multimedia files using MCI and C#
Multimedia can enhance the graphical user interface and the presentation of your application. It is very important that you application is visual appealing to your user, but while not making your application difficult to work with. You can use a sound file to notify a user of an event, or a video file demonstrating what the user must do next. I have wrapped most of the common MCI multimedia command in a class file. This will make it easier for you to use MCI in your applications.
Media Control Interface (MCI)
The Media Control Interface (MCI) provides standard commands for playing multimedia devices and recording multimedia resource files. These commands are a generic interface to nearly every kind of multimedia device. MCI provides applications with device-independent capabilities for controlling audio and visual peripherals. Your application can use MCI to control any supported multimedia device, including waveform-audio devices, MIDI sequencers, CD audio devices, and digital-video (video playback) devices.
mciSendString
The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.
Enter the code
First we import the “winmm.dll” so that we can use the mciSendString function that we need.
DllImport("winmm.dll")
private static extern long mciSendString(string strCommand,
StringBuilder? strReturn, int iReturnLength, IntPtr? hwndCallback);
strCommand
Pointer to a null-terminated string that specifies an MCI command string.
For a list, see Multimedia Command Strings.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_multimedia_command_strings.asp
strReturn
Pointer to a buffer that receives return information. If no return information is needed, this parameter can be null
.
iReturnLength
Size, in characters, of the return buffer specified by the strReturn parameter.
hwndCallback
Handle to a callback window if the "notify" flag was specified in the command string.
The open command
The open command initializes a device. All MCI devices recognize this command.
To send this command, call the mciSendString function with the strCommand parameter set as follows.
public void Open(string sFileName)
{
//mpegvideo is the device type that
//we are going to use
//the format of the song location
//must be with double quotes around it
sCommand = "open \"" + sFileName + "\" type
mpegvideo alias MediaFile";
mciSendString(sCommand, null, 0, IntPtr?.Zero);
}
We are using the mpegvideo device because we can play almost all of the newest media files with it, e.g. divx, xvid, mp3, avi, wav, mpeg, mpg and wmv.
The play command
The play command starts playing a device. CD audio, digital-video, MIDI sequencer, videodisc, VCR, and waveform-audio devices recognize this command. To send this command, call the mciSendString function with the strCommand parameter set as follows.
public void Play()
{
//tell the device to play
sCommand = "play MediaFile";
mciSendString(sCommand, null, 0, IntPtr?.Zero);
}
Conclusion:
I have only covered the basics in this tutorial; there are a lot more MCI function sitting in the wrapper that I wrote. Download the attached source code and demo application to get an idea of how to use the wrapper. You are all welcome to take the wrapper an improve/change it to suit your needs.
Happy coding !!!!
CLICKHERE TO DOWNLOAD SOURCE CODE
0 comments:
Post a Comment