我的项目中没有声音输出 - 附上的onInit代码。尽管经过多次尝试,我无法解决它。朋友们,请帮忙! //导入所有需要的东西 import android.speech.tts.TextToSpeech; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.Locale; //对于这个项目我将Api级别从15改为21 @TargetApi(21)
public class TextToSpeechActivity extends AppCompatActivity
implements TextToSpeech.OnInitListener{
private EditText et;
private TextToSpeech tts;
private Button buttonspeak;
private Button b2;
private int result = 0;
// onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get all the view objects
et = (EditText)findViewById(R.id.et);
buttonspeak = (Button)findViewById(R.id.buttonspeak);
tts = new TextToSpeech(this,this);
// set the listener on the button
buttonspeak.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0){
// Log.e("Hi","Are you OK?");
// this is by way of a helper method
speakOut();
}
});
}
@Override
public void onDestroy(){
// closing down tts
if (tts != null){
tts.stop();
tts.shutdown();}
super.onDestroy();
}
@Override
public void onInit(int status){
if (status == TextToSpeech.SUCCESS){
// set the language to US English
result = tts.setLanguage(Locale.US);
// in case the language setting is not done properly
if(result == TextToSpeech.LANG_MISSING_DATA||result ==
TextToSpeech.LANG_NOT_SUPPORTED ){
Toast.makeText(getApplicationContext(),"Language not
available",Toast.LENGTH_SHORT).show();
buttonspeak.setEnabled(false);}
// if everything is fine
else {buttonspeak.setEnabled(true);}}
else { Log.e("TTS", "Initialisation failed");}}
private void speakOut(){
// get the string typed into the editbox
String text = et.getText().toString();
if (result != tts.setLanguage(Locale.US)){
Toast.makeText(getApplicationContext(),"Enter the right
words",Toast.LENGTH_SHORT).show();}
else{
tts.speak(text, TextToSpeech.QUEUE_FLUSH,null);
// Log.e("Hi","Are you OK?");
}
}
}
答案 0 :(得分:0)
试试这个
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
speakOut();
}
});
}
@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.US);
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);
}
}