在 java 中为 Android 使用 Microsoft Speech API 文字转语音?
Using Microsoft Speech API text-to-speech in java for Android?
我正在尝试在 Android 的 java 项目中使用 Microsoft Speech API 文本转语音。它不工作。是否可以在 java 中使用此 API?
语音转文本功能正常,我发现 the Quickstart 并且使用起来没有问题。
然而,没有java文本转语音的例子,只有C#, C++ (Windows) and 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);
}
}
我在日志中的内容是:
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...
有人可以帮助我吗?
我搜索了 Microsoft Speech API 是否与 Java 兼容,答案是否定的。这似乎也有点明显,因为 Microsoft = C++ / C# 这与 Java 完全不同。
此外,在您的 post 中,您提到了 the Quickstart。这使用认知服务语音 SDK 而不是 Microsoft SAPI.
但是,有原生 Java 库允许文本转语音。这是关于它的 post related to Text-To-Speech engines. Here is more information。
还有一些库可用于 Android:
我会建议一个替代方案。为什么不尝试使用 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();
}
我正在尝试在 Android 的 java 项目中使用 Microsoft Speech API 文本转语音。它不工作。是否可以在 java 中使用此 API?
语音转文本功能正常,我发现 the Quickstart 并且使用起来没有问题。
然而,没有java文本转语音的例子,只有C#, C++ (Windows) and 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);
}
}
我在日志中的内容是:
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...
有人可以帮助我吗?
我搜索了 Microsoft Speech API 是否与 Java 兼容,答案是否定的。这似乎也有点明显,因为 Microsoft = C++ / C# 这与 Java 完全不同。 此外,在您的 post 中,您提到了 the Quickstart。这使用认知服务语音 SDK 而不是 Microsoft SAPI.
但是,有原生 Java 库允许文本转语音。这是关于它的 post related to Text-To-Speech engines. Here is more information。 还有一些库可用于 Android:
我会建议一个替代方案。为什么不尝试使用 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();
}