简体   繁体   English

TextToSpeech无法正常工作

[英]TextToSpeech not working

There is no sound output in my project-- onInit code above attached. 我的项目中没有声音输出-上面的onInit代码。 I am unable to resolve it despite several attempts. 尽管进行了几次尝试,但仍无法解决。 Friends, please help! 朋友们,请帮忙! // import all the needed stuff import android.speech.tts.TextToSpeech; //导入所有需要的内容import android.speech.tts.TextToSpeech; import android.support.v7.app.AppCompatActivity; 导入android.support.v7.app.AppCompatActivity; import android.util.Log; 导入android.util.Log; import android.view.View; 导入android.view.View; import android.widget.Button; 导入android.widget.Button; import android.widget.EditText; 导入android.widget.EditText; import android.widget.Toast; 导入android.widget.Toast; import java.util.Locale; 导入java.util.Locale; //for this project I am changing the Api level to 21 from 15 @TargetApi(21) //对于此项目,我将Api级别从15 @TargetApi(21)更改为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?");
           }

       }
    }

Try this 尝试这个

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);
    }
}

More Reference1 更多参考1

More Reference2 更多参考2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM