Search code examples
javaandroidtext-to-speechsapispeech-synthesis

Using Microsoft Speech API text-to-speech in java for Android?


I am trying to use Microsoft Speech API text-to-speech, in my java project for Android. It's not working. Is it possible to use this API in java?

The speech-to-text is working, I found the Quickstart and had no problem using it.

However, there is no java example for text-to-speech, only in C#, C++ (Windows) and C++ (Linux).

I tried to adapt the code in java, but it's not working and I have no idea why.

 public void onTextToSpeechButtonClicked(View v) {
        TextView txt = (TextView) this.findViewById(R.id.texttospeech); // 'texttospeech' is the ID of my text view

        try {
            // THIS LINE ISN'T WORKING
            com.microsoft.cognitiveservices.speech.internal.SpeechConfig config = com.microsoft.cognitiveservices.speech.internal.SpeechConfig.FromSubscription(speechSubscriptionKey, serviceRegion); 

            config.SetSpeechRecognitionLanguage("fr-FR");
            assert(config != null);

            // Creates a speech synthesizer using the default speaker as audio output
            SpeechSynthesizer synthesizer = SpeechSynthesizer.FromConfig(config);
            assert(synthesizer != null);

            SpeechSynthesizer synthesizer1 = SpeechSynthesizer.FromConfig(config);


            SpeechSynthesisResult result = synthesizer.SpeakTextAsync(txt.toString()).Get();

            // Checks result
            if (result.getReason().equals(ResultReason.SynthesizingAudioCompleted)){
                txt.setText("The text has been said.");
            }
            else if (result.getReason().equals(ResultReason.Canceled)){
                SpeechSynthesisCancellationDetails cancellation = SpeechSynthesisCancellationDetails.FromResult(result);
                txt.setText("CANCELED: Reason ="+cancellation.getReason());
                if(cancellation.getReason().equals(CancellationReason.Error)){
                    txt.append("ErrorCode = "+cancellation.getErrorCode()+" / ErrorDetails = "+cancellation.getErrorDetails()+" / Did you update the subscription info ?");
                }
            }
            synthesizer.delete();

        } catch (Exception ex) {
            Log.e("SpeechSDKDemo", "unexpected " + ex.getMessage());
            assert(false);
        }
    }

What I have in the log is that:

E/ples.quickstar: No implementation found for void com.microsoft.cognitiveservices.speech.internal.carbon_javaJNI.swig_module_init() (tried Java_com_microsoft_cognitiveservices_speech_internal_carbon_1javaJNI_swig_1module_1init and Java_com_microsoft_cognitiveservices_speech_internal_carbon_1javaJNI_swig_1module_1init__)
D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.microsoft.cognitiveservices.speech.samples.quickstart, PID: 4106
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
        at android.view.View.performClick(View.java:6597)
        at...

Can someone help me?


Solution

  • I searched if Microsoft Speech API is compatible with Java and the answer is no. It also seems a little obvious because Microsoft = C++ / C# which is nothing similar to Java. In addition, in your post you mentionned the Quickstart. This uses the Cognitive Services Speech SDK and not Microsoft SAPI.

    However, there are native Java libraries allowing Text-To-Speech. Here is a post related to Text-To-Speech engines. Here is more information about it. There are also libraries available for Android: