C#2008 SP1
我正在使用以下代码录制,播放和停止保存录制内容。一切正常。但是,我想添加一个回调,当回放结束时将会触发。
我使用winmm.dll库进行P / Invoke。
非常感谢您的任何建议。
public partial class SoundTest : Form
{
const uint SND_ASYNC = 0x0001;
const uint SND_FILENAME = 0x00020000;
const uint SND_NODEFAULT = 0x0002;
[DllImport("winmm.dll")]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString,
int returnLength, int hwndCallBack);
[DllImport("winmm.dll")]
private static extern bool PlaySound(string pszsound, UIntPtr hmod, uint fdwSound);
public SoundTest()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Disable stop button
this.btnSaveStop.Enabled = false;
}
private void btnRecord_Click(object sender, EventArgs e)
{
// Disable play and record button
this.btnRecord.Enabled = false;
this.btnPlay.Enabled = false;
// Enable stop button
this.btnSaveStop.Enabled = true;
// Record from microphone
mciSendString("Open new Type waveaudio Alias recsound", "", 0, 0);
mciSendString("record recsound", "", 0, 0);
}
private void btnSaveStop_Click(object sender, EventArgs e)
{
// Enable play and record
this.btnRecord.Enabled = true;
this.btnPlay.Enabled = true;
// Disable Stop button
this.btnSaveStop.Enabled = false;
mciSendString("save recsound c:\\record.wav", "", 0, 0);
mciSendString("close recsound ", "", 0, 0);
}
private void btnPlay_Click(object sender, EventArgs e)
{
//// Diable record button while playing back
//this.btnRecord.Enabled = false;
PlaySound("c:\\record.wav", UIntPtr.Zero, SND_ASYNC | SND_FILENAME | SND_NODEFAULT);
}
}
答案 0 :(得分:1)
据我所知,你能够做到这一点的唯一方法就是在你调用PlaySound API函数并传递一个回调函数后立即调用它。 SND_SYNC参数而不是SND_ASYNC。
private void btnPlay_Click(object sender, EventArgs e)
{
//// Disable record button while playing back
//this.btnRecord.Enabled = false;
PlaySound("c:\\record.wav", UIntPtr.Zero, SND_SYNC | SND_FILENAME | SND_NODEFAULT);
//write your callback code here
}
答案 1 :(得分:1)