我试图通过按下一个按钮来设置 TTS 的参数,例如音高和语速,但是当我按下它时不知何故没有任何变化......
看下面的代码,我的计划是一旦我按下按钮SetPara,_pitch和_rate将设置为 0.5,并且
tts.setPitch(_pitch);
tts.setSpeechRate(_rate);
将 TTS 的音高和语速设置为 0.5,但现在的问题是,在我按下SetPara按钮后,音高和语速没有改变......
请帮帮我:))))
package com.example.text2speechtrail;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
private Button btnSetPara;
private EditText getPitch;
private EditText getRate;
public float _pitch;
public float _rate;
String _string_pitch = null;
String _string_rate = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
btnSetPara = (Button) findViewById(R.id.setPara);
txtText = (EditText) findViewById(R.id.txtText);
getPitch = (EditText) findViewById(R.id.getPitch);
getRate = (EditText) findViewById(R.id.getRate);
btnSetPara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SetPara();
}
});
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
public void SetPara(){
_pitch = (float) 0.5;
_rate = (float) 0.5;
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
tts.setPitch(_pitch);
tts.setSpeechRate(_rate);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}