概要
cscの作法、調べてみた。
dllimportやってみた。
録音してみた。
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Media;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
class form1: Form {
[DllImport("winmm.dll", EntryPoint = "mciSendStringA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int record(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
private SoundPlayer player = null;
private void PlaySound(string waveFile) {
if (player != null) StopSound();
player = new System.Media.SoundPlayer(waveFile);
player.Play();
}
private void StopSound() {
if (player != null)
{
player.Stop();
player.Dispose();
player = null;
}
}
form1() {
Text = "sound";
ClientSize = new Size(400, 400);
Button btn0 = new Button();
btn0.Location = new Point(50, 20);
btn0.Text = "rec";
btn0.Click += btn0_Click;
Button btn1 = new Button();
btn1.Location = new Point(150, 20);
btn1.Text = "stop";
btn1.Click += btn1_Click;
Button btn2 = new Button();
btn2.Location = new Point(250, 20);
btn2.Text = "play";
btn2.Click += btn2_Click;
Controls.AddRange(new Control[] {
btn0,
btn1,
btn2
});
}
void btn0_Click(System.Object sender, System.EventArgs e) {
record("open new Type waveaudio Alias recsound", "", 0, 0);
record("record recsound", "", 0, 0);
}
void btn1_Click(System.Object sender, System.EventArgs e) {
record("save recsound c:\\tmp\\mic.wav", "", 0, 0);
record("close recsound", "", 0, 0);
}
void btn2_Click(object sender, System.EventArgs e) {
PlaySound("c:\\tmp\\mic.wav");
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。