我正在嘗試在我的 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
提交
取消