带语音的 Lex 聊天机器人 C# 客户端

Posted

技术标签:

【中文标题】带语音的 Lex 聊天机器人 C# 客户端【英文标题】:Lex chatbot C# client with voice 【发布时间】:2017-07-11 05:40:55 【问题描述】:

我有运行文本的聊天机器人客户端,但现在想将其更改为语音,但我不确定如何从麦克风获取流以进行发布。为了录制音频,我使用的是 NAudio,但是在发送内存流时出现错误提示

System.IO.IOException:在所有字节都完成之前无法关闭流 写的。

这是我的代码:

private void recordAudio()
        
            if (memoryStream == null)
                memoryStream = new MemoryStream();
            sourceStream = new NAudio.Wave.WaveIn();
            sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(16000, 1);
            waveIn = new NAudio.Wave.WaveInProvider(sourceStream);
            waveWriter = new WaveFileWriter(new IgnoreDisposeStream(memoryStream), waveIn.WaveFormat);
            sourceStream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(sourceStream_DataAvailable);
            buff = new BufferedWaveProvider(waveIn.WaveFormat);
            sourceStream.StartRecording();
            mytimer.Enabled = true;

        
        private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
        
            buff.AddSamples(e.Buffer, 0, e.BytesRecorded);

            Console.WriteLine("test");
        
            void mytimer_Tick(object sender, EventArgs e)
        

            if (sourceStream != null)
            
                sourceStream.StopRecording();
                waveWriter.Flush();



                var amazonLexClient = new AmazonLexClient(Amazon.RegionEndpoint.USEast1);
                var amazonPostRequest = new Amazon.Lex.Model.PostContentRequest();
                var amazonPostResponse = new Amazon.Lex.Model.PostContentResponse();
                amazonPostRequest.BotAlias = "voiceBot";
                amazonPostRequest.BotName = "voiceBot";
                amazonPostRequest.ContentType = "audio/l16; rate=16000; channels=1";
                amazonPostRequest.UserId = "user";
                amazonPostRequest.InputStream = memoryStream;
                amazonPostRequest.UserId = "test";
                try
                
                    amazonPostResponse = amazonLexClient.PostContent(amazonPostRequest);
                    Console.WriteLine("Got a response");
                

                catch (Exception w)
                
                    Console.WriteLine("0 Exception caught.", e);
                    Console.WriteLine(w.Message);
                

【问题讨论】:

尝试先发布现有的音频文件,然后再录制。 Cannot close stream until all bytes are written (GoodData API)的可能重复 【参考方案1】:

在将Position of your MemoryStream 传递给发布请求之前,您必须将其设置为 0。

memoryStream.Position = 0;

【讨论】:

以上是关于带语音的 Lex 聊天机器人 C# 客户端的主要内容,如果未能解决你的问题,请参考以下文章

几分钟带你快速了解聊天机器人!

如何优雅的用Python玩转语音聊天机器人

❤️Hi! Azure.❤️从零开始打造一个语音机器人,跟你的电脑聊聊天!❤️

语音聊天

学习笔记TF059:自然语言处理智能聊天机器人

python 实现简单语音聊天机器人