我正在尝试在我的 Android 的 java 项目中使用 Microsoft Speech API text-to-speech。它不工作。是否可以在 java 中使用此 API?语音转文本工作正常,我找到了快速入门并且使用它没有问题。但是,没有用于文本到语音的 java 示例,仅在C#、C++ (Windows)和C++ (Linux)中。我试图在java中调整代码,但它不起作用,我不知道为什么。 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); } }有人能帮我吗?
2 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
我会建议一个替代方案。为什么不尝试使用 windows tts,而是将文本发送到可以从 java 代码执行的 powershell 命令:
String[] command = {"powershell.exe", "Add-Type -AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak('" + message +"');"};
try {
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
System.out.format("Process exit code: %d", process.waitFor());
ProcessInputOutput.readStdnOutput(process);
}
catch (Exception e){
e.printStackTrace();
}
添加回答
举报
0/150
提交
取消