【发布时间】:2021-07-04 11:39:52
【问题描述】:
-
我还实现了一个
EditText字段,在该字段中检索结果。我正在尝试Text-To-Speech或TTS。 -
我正在使用
Log.i("Recognitionss", String.valueOf(results.get(0).getTitle()));将结果记录到DetectorActivity.java -
生成的日志结果如下所示,我已将其传递给
XML文件
2021-04-14 19:34:24.826 29814-29924/org.tensorflow.lite.examples.detection I/Recognitionss: L
2021-04-14 19:34:24.835 29814-29924/org.tensorflow.lite.examples.detection I/tensorflow: MultiBoxTracker: Processing 1 results from 123
2021-04-14 19:34:24.855 29814-29814/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Preparing image 128 for detection in bg thread.
2021-04-14 19:34:24.876 29814-29924/org.tensorflow.lite.examples.detection I/tensorflow: DetectorActivity: Running detection on image 128
- 我已将
Button和EditText添加到我的bottom sheet导航中。
<Button
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="10dp"
android:text="Voice Conversion" />
<EditText
android:id="@+id/txtText"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"/>
-
TTS的 Java 代码
package org.tensorflow.lite.examples.detection;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public class AndroidTextToSpeechActivity extends AppCompatActivity {
Button btnSpeak;
EditText editText;
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tfe_od_layout_bottom_sheet);
// Init TextToSpeech
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(getApplicationContext(), "This language is not supported!",
Toast.LENGTH_SHORT);
} else {
btnSpeak.setEnabled(true);
textToSpeech.setPitch(0.6f);
textToSpeech.setSpeechRate(1.0f);
speak();
}
}
}
});
// Init View
btnSpeak = (Button)findViewById(R.id.btnSpeak);
editText = (EditText)findViewById(R.id.txtText);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
speak();
}
});
}
private void speak() {
String text = editText.getText().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
@Override
protected void onDestroy() {
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
- 问题是当我点击按钮时没有语音反馈,控制台或logcat中也没有错误。
【问题讨论】:
-
你能记录这个 _public void onInit(int status) { _ status value 来检查你得到什么状态
-
@kelvin 我已经用我的解决方案更新了我的问题,所以我将不再实施任何东西,因为我已经得到了我想要的输出。
-
所以只需将其作为答案发布并接受您自己的答案,以免引起注意。
-
好的,谢谢。对造成的不便深表歉意。
标签: java android tensorflow text-to-speech gesture-recognition