It depends on what do you call "not using library". Isn't the OS API a kind of a library? :-) Anyway…
The simplest way (and very outdated but working one) is to use the Windows module "winmm.dll" via P/Invoke. Here is how:
using System;
using System.Runtime.InteropServices;
internal static class Mci {
internal const string DllName = "winmm.dll";
internal const string SoundRecordingDeviceId = "soundRecordingDevice";
internal const string OpenCommandFormat = "open new type waveaudio alias {0}";
internal const string RecordCommandFormat = "record {0}";
internal const string PauseCommandFormat = "pause {0}";
internal const string StopCommandFormat = "stop {0}";
internal const string CloseCommandFormat = "close {0}";
internal const string SaveCommandFormatFormat = @"save {0} ""{{0}}""";
internal static readonly string OpenRecorderCommand = string.Format(OpenCommandFormat, SoundRecordingDeviceId);
internal static readonly string RecordCommand = string.Format(RecordCommandFormat, SoundRecordingDeviceId);
internal static readonly string PauseCommand = string.Format(PauseCommandFormat, SoundRecordingDeviceId);
internal static readonly string StopCommand = string.Format(StopCommandFormat, SoundRecordingDeviceId);
internal static readonly string CloseRecorderCommand = string.Format(CloseCommandFormat, SoundRecordingDeviceId);
internal static readonly string SaveCommandFormat = string.Format(SaveCommandFormatFormat, SoundRecordingDeviceId);
[DllImport(DllName)]
private static extern long mciSendString(
string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr oCallback);
internal static void Open() {
mciSendString(OpenRecorderCommand, null, 0, IntPtr.Zero);
}
internal static void Record() {
mciSendString(RecordCommand, null, 0, IntPtr.Zero);
}
internal static void Pause() {
mciSendString(PauseCommand, null, 0, IntPtr.Zero);
}
internal static void Stop() {
mciSendString(StopCommand, null, 0, IntPtr.Zero);
}
internal static void Close() {
mciSendString(CloseRecorderCommand, null, 0, IntPtr.Zero);
}
internal static void SaveRecording(string fileName) {
mciSendString(string.Format(SaveCommandFormat, fileName), null, 0, IntPtr.Zero);
}
}
It's tested.
Important note: You need to call
SaveRecording
before calling
Close
. You first open the device, record/pause/resume… (to resume, call
Record
again) and stop, then you save the file and then you can open/record again.
This is kind of a lame to use string commands using this interface, but this is what I immediately have in my hands; and it works fast enough, so it's not a problem to use it. It can only record from a default recording device. The output file will be a WAV stereo file with "standard" wave parameters.
In many cases, you may need a lot more features. In this case, you would need to use the assembly "DirectD.DirectSound.dll". You can add the reference in the tab .NET of the "Add Reference" window, because this is the assembly put in the GAC. Using it is also pretty easy. Please start here:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb318690%28v=vs.85%29.aspx[
^].
—SA