Reputation: 271150
I am developing an app that requires text to speech. So I read a few tts tutorials and did this:
public void buttonClick (View view) {
TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
@Override
public void onInit(int status) {
}
});
tts.setLanguage (Locale.UK);
tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}
However, when I run the app and click the button, no voice is heard, only the "click" sound of the button. This also implies that the speaker is switched on.
I guess this is because speak(String, int, HashMap<String, String>)
is deprecated. This guess is proved wrong when I look at the docs:
/** @deprecated As of API level 21, replaced by
* {@link #speak(CharSequence, int, Bundle, String)}.
*/
And my app's minimum SDK version is API18 and my device's android version is Android 4.3. That means speak
is not deprecated. The deprecation is maybe just a bug of Android Studio.
I wonder why it makes no sound and how I can fix this problem.
Upvotes: 1
Views: 2104
Reputation: 3874
public void buttonClick (View view) {
TextToSpeech tts = new TextToSpeech (this, new TextToSpeech.OnInitListener () {
@Override
public void onInit(int status) {
// change required.Initialization has to finish first.
if(status != TextToSpeech.ERROR) {
tts.setLanguage (Locale.UK);
tts.speak ("Hello World", TextToSpeech.QUEUE_FLUSH, null);
}
}
});
}
Upvotes: 1
Reputation: 3025
try this example.
import android.app.Activity;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import java.util.List;
import java.util.Locale;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.widget.Toast;
public class MainActivity extends Activity {
TextToSpeech t1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.speak("Hi dear its working", TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
and xml file is
<RelativeLayout 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"
tools:context=".MainActivity"
android:transitionGroup="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to Speech"
android:id="@+id/button"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
</RelativeLayout>
Upvotes: 1