Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

CognitiveServices.Speech Blazor Server not working when Published to Azure

I am using Microsoft.CognitiveServices.Speech in a Blazor Server App and I can convert Text to Speech and hear the voice while running the App in VS2019 on my PC but If I publish it to Azure I don't hear any sound, Any ideas?

Thx

question from:https://stackoverflow.com/questions/65599466/cognitiveservices-speech-blazor-server-not-working-when-published-to-azure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I test this service with an asp.net core MVC project converting text to speech, and got the same situation with you.

The void play automatically after converting locally, but not played on Portal. I check if the .wav file created or not in Kudu. It shows the file had been created, which means it's not a problem it didn't play, you could download it to listen. enter image description here

By the way, I use the sample code, just modify the path to store .wav file:

    public async Task SynthesisToSpeakerAsync(string text)
    {
        // Creates an instance of a speech config with specified subscription key and service region.
        // Replace with your own subscription key and service region (e.g., "westus").
        // The default language is "en-us".
        var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion");
        string path = string.Empty;
        string webRootPath = _hostingEnvironment.WebRootPath;
        string contentRootPath = _hostingEnvironment.ContentRootPath + "/MyMedia/";
        string filename = "sample.wav";
        // Try to create the directory.
        //DirectoryInfo di = Directory.CreateDirectory(contentRootPath);
        // Creates a speech synthesizer using the default speaker as audio output.
        using (var synthesizer = new SpeechSynthesizer(config))
        {
            // Receive a text from console input and synthesize it to speaker.
            Console.WriteLine("Type some text that you want to speak...");
            Console.Write("> ");

            using (var result = await synthesizer.SpeakTextAsync(text))
            {
                if (result.Reason == ResultReason.SynthesizingAudioCompleted)
                {
                    Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
                    var stream = AudioDataStream.FromResult(result);
                    await stream.SaveToWaveFileAsync(contentRootPath+filename);
                    Console.WriteLine("Successfully saved");
                }
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails=[{cancellation.ErrorDetails}]");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
        }
    }

Use this api to invoke:

public string test(string txt)
    {
        SynthesisToSpeakerAsync(txt);
        return "";
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...