private void Recorder_ExecuteCode(object sender, EventArgs e)
{
fileName = "";
SendMail = false;
CallIsAlive = true;
Recorder recorder = new Recorder();
// Use the call ID as the filename and create a media sink.
Random rnd = new Random();
fileName = string.Format("C:\\recordings\\Inbound Call\\EmergencyCall {0}.wma", DateTime.Now.Day.ToString() + "." + DateTime.Now.Month.ToString() + "." + DateTime.Now.Year.ToString() + " - " + DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString() + rnd.Next().ToString());
WmaFileSink fileSink = new WmaFileSink(fileName);
Console.WriteLine("Recording for 10 seconds.");
// Set the recorder to use the media sink and attach
// it to the flow.
recorder.SetSink(fileSink);
recorder.AttachFlow(Program.MyCall.Flow);
// Start recording.
recorder.Start();
SendMail = true;
}
上面的代码设置了一个附加到入站呼叫的音频视频流的记录器。
我遇到的问题是,如果同时有 2 个调用并尝试调用 Recorder_executeCode 函数,则程序在此处崩溃:
recorder.AttachFlow(Program.MyCall.Flow);
Visual Studio 报告以下错误:
用户代码未处理 InvalidOperationException。AudioVideoFlow 已绑定到 Recorder。
如果两个或多个调用者同时调用,我将如何更改代码以允许函数的多个副本同时运行。
编辑
问题是由使用全局变量引起的:MyCall
我通过使用 Call.CallId 设置文件名字符串解决了这个问题
private void Recorder_ExecuteCode(object sender, EventArgs e)
{
string fileName;
SendMail = false;
Recorder recorder = new Recorder();
// Use the call ID as the filename and create a media sink.
//Random rnd = new Random();
fileName = string.Format("C:\\recordings\\Inbound Call\\EmergencyCall {0}.wma", acceptCallActivityInboundCall1.CallProvider.ToneController.AudioVideoFlow.Call.CallId.ToString());
WmaFileSink fileSink = new WmaFileSink(fileName);
Console.WriteLine("Recording for 10 seconds.");
// Set the recorder to use the media sink and attach
// it to the flow.
recorder.SetSink(fileSink);
recorder.AttachFlow(acceptCallActivityInboundCall1.CallProvider.ToneController.AudioVideoFlow);
// Start recording.
recorder.Start();
SendMail = true;
}