[C#] mciSendString多线程操作录音失败,返回263错误码

使用mciSendString进行录音,利用Timer定时器,一段时间后结束录音,保存录音文件,但是没有录音文件生成,查看mciSendString返回值,返回263(错误码对应的错误信息为INVALID_DEVICE_NAME)。查询资料后有人说mciSendString不支持多线程操作,在codeproject上找到解决方案,记录并分享给大家。

codeproject上页面https://www.codeproject.com/questions/183548/how-to-use-mcisendstring-in-a-threaded-application 中SebaSOR提供的处理方式

using System;
using System.Threading;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class mciSafeCall
{
    [DllImport("winmm.dll")]
    private static extern int mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

    private int syncMciResult;
    private string syncMciCommand;
    private Form formulario;

    public mciSafeCall(Form form)
    {
        formulario = form;
        syncMciCommand = "";
    }

    public void mciReplacement(string Command)
    {
        syncMciCommand = Command;
        if (!string.IsNullOrEmpty(syncMciCommand))
        {
            if (formulario.InvokeRequired)
                formulario.Invoke(new Action(() =>
                {
                    syncMciResult = mciSendString(syncMciCommand, null, 0, IntPtr.Zero);
                }));
            else
                syncMciResult = mciSendString(syncMciCommand, null, 0, IntPtr.Zero);

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中设置时间录音,可以使用Windows提供的Win32 API函数来控制录音设备。以下是一个简单的示例: ```csharp using System; using System.Runtime.InteropServices; using System.Threading; class Program { // Win32 API函数声明 [DllImport("winmm.dll", SetLastError = true)] static extern uint timeSetEvent(uint uDelay, uint uResolution, TimerCallback lpTimeProc, IntPtr dwUser, uint fuEvent); [DllImport("winmm.dll", SetLastError = true)] static extern uint timeKillEvent(uint uTimerID); [DllImport("winmm.dll", SetLastError = true)] static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, IntPtr hwndCallback); // 录音参数 const int SampleRate = 44100; const int BitsPerSample = 16; const int Channels = 2; static void Main(string[] args) { // 设置录音时长为5秒 int duration = 5 * 1000; // 准备录音 string command = $"open new type waveaudio alias recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音格式 command = $"set recsound format tag pcm bitspersample {BitsPerSample} channels {Channels} samplespersec {SampleRate}"; mciSendString(command, null, 0, IntPtr.Zero); // 开始录音 command = $"record recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音时长 uint callbackId = timeSetEvent((uint)duration, 0, TimerCallback, IntPtr.Zero, 1); // 等待录音结束 Thread.Sleep(duration); // 停止录音 command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 取消时间事件 timeKillEvent(callbackId); } // 时间事件回调函数 static void TimerCallback(uint uTimerID, uint uMsg, IntPtr dwUser, IntPtr dw1, IntPtr dw2) { // 停止录音 string command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); } } ``` 在上面的示例中,我们使用timeSetEvent函数来设置录音时长,然后使用mciSendString函数来准备录音设备,设置录音格式,开始录音,停止录音和关闭录音。在时间事件回调函数中,我们停止录音并关闭录音设备。 请注意,由于使用了Win32 API函数,需要添加System.Runtime.InteropServices命名空间,并且该代码只能在Windows操作系统上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值