【发布时间】:2020-10-20 22:26:25
【问题描述】:
我在Google documentation page 上找到了这段代码(Android Studio 自动更改了一点):
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void ssmlToAudio(String ssmlText, String outFile) throws Exception {
// Instantiates a client
try (TextToSpeechClient textToSpeechClient = TextToSpeechClient.create()) {
// Set the ssml text input to synthesize
SynthesisInput input = SynthesisInput.newBuilder().setSsml(ssmlText).build();
// Build the voice request, select the language code ("en-US") and
// the ssml voice gender ("male")
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.MALE)
.build();
// Select the audio file type
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
// Perform the text-to-speech request on the text input with the selected voice parameters and
// audio file type
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
// Write the response to the output file
try (OutputStream out = new FileOutputStream(outFile)) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file " + outFile);
}
}
}
我想在点击事件上运行这个方法。所以这是我迄今为止尝试过的:
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void onClick(View view) throws Exception {
ssmlToAudio("Hello", "test");
}
但是如果我运行我的应用并点击一个按钮,我会得到这个错误:
java.lang.IllegalStateException: 无法执行方法 android:onClick
我做错了什么?
【问题讨论】:
-
你必须在你的activity中实现
onClickListener,然后重写onClick方法。 -
@ReazMurshed 谢谢!这解决了我的问题。
-
很高兴知道这一点!乐意效劳!将此作为答案发布。
标签: java android google-text-to-speech