microsoft / cognitive-services-sdk-react-native-example

Example repo integrating the JavaScript Speech SDK with a react native that runs on Android and iOS
19 stars 9 forks source link

No audio output with SpeechSynthesizer #11

Open asciiman opened 2 years ago

asciiman commented 2 years ago

Hello,

I am trying to get SpeechSynthesizer working in React Native. I set up code as outlined in this example. It looks like the audio is running. I get the +++ onAudioEnd console log after a moment. However, I hear no sound. I tried this on the simulator and a real device and of course made sure that the volume was turned up.

const speechConfig = SpeechConfig.fromSubscription(
      this.state.subscriptionKey,
      this.state.region,
    );
    speechConfig.speechSynthesisLanguage = 'en-US';
    speechConfig.speechSynthesisVoiceName = 'en-US-JennyMultilingualNeural';

    const player = new SpeakerAudioDestination();
    player.onAudioEnd = () => {
      console.log('+++ onAudioEnd');
    };

    const audioConfigOutput = AudioConfig.fromSpeakerOutput(player);

    const synthesizer = new SpeechSynthesizer(speechConfig, audioConfigOutput);

    synthesizer.speakSsmlAsync(
      `<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
        <voice name="en-US-AshleyNeural">
          <mstts:express-as style="normal">
          ${'Hello, here is my question!'}
          </mstts:express-as>
        </voice>
      </speak>`,
      result => {
        if (result) {
          synthesizer.close();
          return result.audioData;
        }
        return undefined;
      },
      error => {
        synthesizer.close();
        console.log(
          '+++ error',
          new Error(`Error while saying question: ${error}`),
        );
      },
    );

This code was pasted inside the existing startRecognition() function in place of the existing code in that function.

Any ideas?

nitesh-b commented 9 months ago

The code documentation mentioned: const audioConfigOutput = AudioConfig.fromSpeakerOutput(player); is only for browsers. I am using expo-av to play audio buffer.

` async function playAudioFromArrayBuffer(arrayBuffer: ArrayBuffer) { const audio = new Audio.Sound()

try {
  // Convert the ArrayBuffer to a base64-encoded string
  const base64Data = await arrayBufferToBase64(arrayBuffer)

  // Load the audio from the base64 string
  await audio.loadAsync({ uri: `data:audio/wav;base64,${base64Data}` })

  // Play the loaded audio
  await audio.playAsync()
} catch (error) {
  console.error("Error playing audio:", error)
}

} `

rajaroni commented 6 months ago

 // const speechConfig = SpeechConfig.fromSubscription("avhvhjdhajhajahajaahaj", "eastus");
    // let audioContext = new AudioContext();
    // let bufferSource = audioContext.createBufferSource();
    const speechConfig = sdk.SpeechConfig.fromSubscription("------------------", "eastus");
    const audioConfig =  sdk.AudioConfig.fromDefaultSpeakerOutput();
    let synthesizer = new sdk.SpeechSynthesizer(speechConfig, audioConfig);
    let text = 'Hello how are you young man';
    synthesizer.speakTextAsync(text, result => {
      console.log(result.audioData);
      return result.audioData
    }, error => {
       console.log({error});
    });

i am using this code on react native my result,audio data is empty array and i didn't hear any voice any help ?