如何将响应从Google Cloud Text-To-Speech流连接到NAudio库

问题描述

我想听声音,但我不想创建文件mp3。这是在具有.NET的桌面应用程序中。

我可以从Google Cloud中读取响应,然后创建文件mp3。我可以将该文件发送到NAudio库,然后进行播放。但是我需要跳过创建mp3文件的步骤。

你能帮我吗?谢谢。

    Dim credential_path As String = "C:\DESARROLLO\MyBrain\MyBrain2.json"
    System.Environment.SetEnvironmentvariable("GOOGLE_APPLICATION_CREDENTIALS",credential_path)

    Dim client As TextToSpeechClient = TextToSpeechClient.Create()
    Dim input As SynthesisInput = New SynthesisInput With {
        .Text = "Hello,World!"
    }
    Dim voice As VoiceSelectionParams = New VoiceSelectionParams With {
        .LanguageCode = "en-US",.SsmlGender = SsmlVoiceGender.Neutral
    }
    Dim config As AudioConfig = New AudioConfig With {
        .AudioEncoding = AudioEncoding.Mp3
    }

    ' Response from Google Cloud
    Dim response = client.SynthesizeSpeech(New SynthesizeSpeechRequest With {
        .Input = input,.Voice = voice,.AudioConfig = config
    })

    ' Create MP3
    Using output As Stream = File.Create("sample.mp3")
        response.AudioContent.Writeto(output)
        Console.WriteLine($"Audio content written to file 'sample.mp3'")
    End Using

    ' How connect the response from Google Cloud Text-To-Speech (Stream) to the NAudio Library
    Using ms As Stream = File.OpenRead("sample.mp3") ' response
        Using rdr = New Mp3FileReader(ms)

            Using wavStream = WaveFormatConversionStream.CreatePcmStream(rdr)

                Using baStream = New BlockAlignReductionStream(wavStream)

                    Using waveOut = New WaveOut(WaveCallbackInfo.FunctionCallback())
                        waveOut.Init(baStream)
                        waveOut.Play()

                        While waveOut.PlaybackState = PlaybackState.Playing
                            Thread.Sleep(100)
                        End While
                    End Using
                End Using
            End Using
        End Using
    End Using

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...