Multimedia is the best way to enhance the user interface of any application. In .NET there is many ways to play an Audio using any .NET technology supporting languages. One of the simple way to acheive is Multimedia Control Interface, shirtly we call it as MCI, wrapping high-level system components, such as Windows Media Player to play our audio files.
About Media Control Interface (MCI):The Media Control Interface (MCI) provides standard commands for playing resource files. MCI's standard commands are a generic interface to almost all kind of multimedia devices. Media Control Interface provides applications with device-independent capabilities for controlling audio. MCI - High level multimedia control functions, Has commands common to all multimedia hardware. Possible since most use record/play metaphor Open a device for input or output If input, record; If output, play When done, close.
There are two different forms of MCI, one is Send command messages (like Windows messages) to MCI (need to include bit-encoded flags and C data structures) and another one is Send text strings to MCI Good for use from scripting languages with string functionality and simple to use MCI converts them to command messages.
Sending Strings to MCI:
error = mciSendString(sCmd , sRetStr, iReturn, hCallback);
· sCmd--themci command string (specifies command & device)
· sRetStr--return string buffer (NULL if none used)
· iReturn--size of return string buffer (0 if none used)
· hCallback--Handle to Callback window (NULL if none used)
– Returns 0 if command is successful, error code if not
· Can be used as a parameter to mciGetErrorString()
– Many command strings possible
– mciSendString, mciGetErrorString
– MCI Command Strings
For more details or help check the
MSDN.
When Using Win32 Functions From .NET
Important note, MCI is not directly accessible from .NET, also mciSendString() is C++, not C#, but still you can use MCI and other Win32 API functions from .Net languages. The key is to use “Platform Invocation Services” “Interop Services” A generalized mechanism that allows calling functions that are imported from DLLs.
Must include: System.Runtime.InteropServices namespace; And then prefix any declarations of Win32 API functions to be used.
Example:
[DllImport("winmm.dll")]
private static extern long mciSendString(
string strCommand,
StringBuilder strReturn,
int iReturnLength,
IntPtr oCallback);For MCI functions that DLL is winmm.dll, And then use equivalent .NET language data types for the parameters and for the type returned by the function.
Some of the disadvantages to be notes:
Code is no longer managed code and it’s no longer platform independent.
mciSendString() in .NET Unmanaged Code:Corresponding C# parameter types would be:
– string, string, uint, intPtr
– In C# DWORD is implemented as an int
strCommand - Pointer to a null-terminated string that specifies an MCI command string.
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.
Some MCI Command String Commands:
· open -- initializes a multimedia device
· play– starts playing an open device
· stop -- stops playing from an open device
· record -- starts recording to a device
· seek -- move to a specified position on device
· save -- saves an MCI file
· close -- closes a device and associated resources
· set -- establish control settings for the device
· status -- returns information in 2nd parameter
However, most of these libraries are designed in such a way that they cannot be used to manipulate audio samples on the fly because they do not give you access to the actual sound data.
Here in this article i have covered some basics about the MCI in .NET. You can explore more in MSDN or in any other resource centre.
>>Download The Sample Code>>