Using C#.NET play multimedia files - Using Multimedia Control Interface(MCI)

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

Comments

Ibrahim Dwaikat said…
If i have more than one sound card, how can I choose the output soundcard of them using MCI???
Randhir said…
I have run application on the web server then problem not save the file on another system i.e using the concept on Website in asp.net Technologies.Pls help me.....

1. Open C#.net web applications. And added the blow namespace.

using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;

2. Add the below API.
[DllImport(”winmm.dll”, EntryPoint = “mciSendStringA”, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

3. Create three Buttons and given the below name and text for the buttons.

1. Record
2. SaveStop
3. Read

1. Under Record Button Click paste the below Code:

// record from microphone
mciSendString(”open new Type waveaudio Alias recsound”, “”, 0, 0);
mciSendString(”record recsound”, “”, 0, 0);

2. Under Save / Stop button Click,

// stop and save
mciSendString(”save recsound c:\\record.wav”, “”, 0, 0);
mciSendString(”close recsound “, “”, 0, 0);
Computer c = new Computer();
c.Audio.Stop();

3. Under Read Button Click

Computer computer = new Computer();
computer.Audio.Play(”c:\\record.wav”, AudioPlayMode.Background);
Adnan Aman said…
Can i deoploy my application on xp.. actually i did but it didn't work. please guide me how can i make it work.
Abdulla T S said…
hi,myself abdulla.i have encountered an error when i use web application is that
Using Microsoft.VisualBasic.Devices
it tell the error that u are miising an assembly reference or .Devices is not recof=gnized.please help me to complete the project
Megha said…
abdulla, you should add reference to microsoft.visualbasic assembly from add reference menu item in solution explorer. it should work then.
Rehan said…
I am using windows 7 and this code isn't working for me as when i am going to read this file .. it throws exception that file doesn't exist which means it is not saving my audio!! any help is highly appreciated .. thanks
Rehan said…
I am using windows 7 and this code isn't working for me as when i am going to read this file .. it throws exception that file doesn't exist which means it is not saving my audio!! any help is highly appreciated .. thanks

Popular posts from this blog

ASP.NET website "Cannot read configuration file due to insufficient permissions" in IIS 7 and Windows Server 2008

Error occurred in deployment step 'Install app for SharePoint': The System Account cannot perform this action.

Embrassing screen sharing interview : SharePoint