Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have ASP.Net MVC application which is using SignalR to pass data to all connected client. I need to read out this data received on Client in English and Arabic. Client browser can be on iOS or Android or Windows PC. How can i solve this issue.

1.Is there any proper way to convert text to speech from server and pass output audio to client in sequence
2.Or combine audio snippets and play on client

Any other better solution?

What I have tried:

I tried Jquery speech synthesis.But it does not support Arabic in Android and iOS.My application will be Offline.So using third party API from Google or Amazon is out of scope.
Posted
Updated 18-Dec-20 19:16pm

1 solution

The Javascript speech synthesis API works in all modern browsers:
SpeechSynthesis - Web APIs | MDN[^]

The supported languages will vary depending on the browser. For example, on my PC, Firefox only supports English, but Chrome (and by extension other Chromium-based browsers) supports many more languages.
Using the Web Speech API for Multilingual Translations | CSS-Tricks[^]

If you prefer to generate the audio file on the server, you should be able to combine the Audio API[^] with a Blob URL[^] to play the file. For example:
Play audio via fetch POST request - Jignesh Kakadiya - Medium[^]
JavaScript
async function speak(text, language) {
    const url = "/text-to-speech/";
    
    const requestOptions = {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ text: text, language: language })
    };
    
    let response = await fetch(url, requestOptions);
    if (!response.ok) {
        throw new Error(`${response.status}: ${response.statusText}`);
    }
    
    var reader = response.body.getReader();
    var result = await reader.read();
    
    var blob = new Blob([result.value], { type: 'audio/mp3' });
    var url = window.URL.createObjectURL(blob);
    window.audio = new Audio();
    window.audio.src = url;
    window.audio.play();
}
 
Share this answer
 
v2
Comments
tombell11 13-Sep-23 2:18am    
my name is tombel

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900