Issue
I am now trying to implement a function that when the user clicks the button, it will speak input words inside the EditText. But I could not find how to improve my codes, any helps or advice, please.
Following this youtube video Working with TTS in Android Studio
As I checked some sites, I tried everything, for instance,
・Change the emulator made 3 emulators
・Setting->System->Languages & input->Advanced->Text-to-speech output
・Re-install English(US)
・Preferred engine is Google Text-to-speech Engine
・Check the SDK version, and it is 30
Finally realized that 「W/TextToSpeech: speak failed: not bound to TTS engine」 appears in the android studio
↓MainActivity.java
package com.example.sounttest2;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{
int MY_DATA_CHECK_CODE = 1000;
TextToSpeech textToSpeech;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button_tts);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = editText.getText().toString();
if(text.length() > 0){
textToSpeech.speak(text,TextToSpeech.QUEUE_ADD, null);
}
}
});
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(intent, MY_DATA_CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
textToSpeech = new TextToSpeech(this, this);
} else {
Intent intent = new Intent();
intent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(intent);
}
}}
@Override
public void onInit(int i) {
if(i == textToSpeech.SUCCESS){
Toast.makeText(this,"Success",Toast.LENGTH_SHORT).show();
} else if (i == textToSpeech.ERROR){
Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText"
android:hint="@string/hint"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_tts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/button" />
</LinearLayout>
↓strings.xml
<resources>
<string name="app_name">Test</string>
<string name="hint">"Please Input Something"</string>
<string name="button">Text To Speech</string>
</resources>
Solution
If your are using sdk version 30 you need to go back on 28 it will work
Answered By - Azmat Ali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.