线程无法正常工作 [英] Threading doesn't work correctly

查看:104
本文介绍了线程无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它可以重复听到的数字,然后记录我所读的数字.数字为0,1,2,3,4,5,6,7,8,9.我使用一个for循环来播放这些数字,并具有来自第三方的文字转语音技能.

I have an application, which is to repeat the numbers what I hear then record the digits what I read. The numbers are 0,1,2,3,4,5,6,7,8,9. I use a for loop to play these numbers with a text to speech skill, which is from a third party.

对于记录部分,我必须根据第三方要求将其放在单独的线程中.要录制声音,该方法可能是:

For the recording part, I have to put it in a separate thread by the third party requirement. To record the voice, the method is likely:

 recordVoiceResource.Record(fileName);

要停止它,请使用:

 recordVoiceResource.Stop();

现在我发现有时我的录音为0秒,这意味着代码可能未到达Record行.有时只有2秒.我认为线程调度是错误的.

Now I find sometimes my recording is 0 second, which means the code perhaps doesn't reach Record line. Sometimes it only has 2 seconds. I believe the thread schedule is wrong.

 private ManualResetEvent terminate = new ManualResetEvent(false);

 PlayTTS("Please repeat the following numbers as you hear them.");

                Thread t = new Thread(() => RecordNumbers());
                t.Start();
                Thread.Sleep(2000);
                terminate.Set();
                terminate.WaitOne();
                PlayNumbers();
                recordVoiceResource.Stop();

线程方法是:

    private void RecordNumbers()
    {
        recordVoiceResource = TelephonyServer.GetVoiceResource();
        recordVoiceResource.MaximumSilence = 1;
        recordVoiceResource.MaximumTime = 30;
        // Start recording what I read from I heard
        recordVoiceResource.Record(fileName);
    }

要播放号码,

    private void PlayNumbers()
    {
        foreach (var item in numbers)
        {
            try
            {
                vr.PlayTTS(item.ToString()); // will be 0,1,2,...9
                Thread.Sleep(2000);
            }

推荐答案

根据您的评论,属性MaximumSilence获取或设置以秒为单位的最大静默状态,直到下一个语音功能终止为止.您将其设置为一秒钟,开始记录,然后在开始播放之前睡眠两秒钟,提示用户说些什么.您在这里看到问题了吗?假设在此期间麦克风没有听到一些无关的语音,录音将在播放开始之前停止.

According to your comment, the property MaximumSilence gets or sets the maximum silence in seconds that will be allowed until termination of the next voice function. You are setting it to one second, starting the recording, and then sleeping for two seconds before beginning playback that prompts the user to say something. Do you see the problem here? Assuming the mic doesn't pick up some unrelated speech during that period, the recording will stop before the playback even begins.

由于数字播放之间存在2秒的间隔,因此您可能需要将MaximumSilence设置为几秒钟.

Since there is a 2-second gap between number playback, you probably need to set MaximumSilence to several seconds.

当然,这就是假设您的意图是捕获讲所有数字的用户的单个记录(这就是您的代码的编写方式).如果要单独捕获语音号码,则在播放每个号码时可能需要安排并同步单独的录音.您可能需要仔细检查API,以确保您的解决方案符合您的预期.

That is, of course, assuming your intention was to capture a single recording of the user speaking all the numbers (which is how your code is written). If you want to capture the spoken numbers individually, then you may need to schedule and synchronize separate recordings as each number is played back. You may want to double-check the API to make sure your solution is what you intended.

这篇关于线程无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆