带有语音功能的Lex chatbot C#客户端 [英] Lex chatbot C# client with voice

查看:100
本文介绍了带有语音功能的Lex chatbot C#客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the chatbot client running with text but would now like to change it to voice but I am unsure of how to get the stream from the mic for post. For recording audio I am using NAudio but when sending the memory stream I get an error stating

System.IO.IOException:在关闭所有字节之前无法关闭流

System.IO.IOException: Cannot close stream until all bytes are written.

这是我的代码:

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);
                }

推荐答案

您必须设置

You have to set the Position of your MemoryStream to 0 before you pass it to the post request.

memoryStream.Position = 0;

这篇关于带有语音功能的Lex chatbot C#客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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