【问题标题】:TextToSpeech not workingTextToSpeech 不工作
【发布时间】:2017-12-27 01:19:08
【问题描述】:

我的项目中没有声音输出——附上上面的 onInit 代码。尽管多次尝试,我无法解决它。朋友,请帮忙! // 导入所有需要的东西 导入 android.speech.tts.TextToSpeech; 导入android.support.v7.app.AppCompatActivity; 导入android.util.Log; 导入android.view.View; 导入android.widget.Button; 导入 android.widget.EditText; 导入 android.widget.Toast; 导入 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?");
           }

       }
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个

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

    更多Reference1

    更多Reference2

    【讨论】:

    • 我确实调用了 speakOut()。我的其余代码如下:
    • import---- @TargetApi(21) public class --- protected void onCreate(---) { et = (EditText)findViewById(R.id.et); buttonpeak = (Button)findViewById(R.id.buttonspeak); tts = new TextToSpeech(this,this); buttonpeak.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0){ speakOut(); }}); } -- public void onDestroy(){ if (tts != null){ tts.stop(); tts.shutdown();} super.onDestroy(); }
    • @Venugopal 请更新您的 cmets 问题实例
    • @Venugopal 请发布您的完整代码,以便我可以帮助您您在哪里出错
    • 感谢 Gowtham 抽出时间来回答我的问题。我的问题是当我粘贴整个代码时,网站说它太长了。当我尝试在 cmets 中添加剩余代码时,格式消失了!这有什么办法吗?我是一个初学者,你现在猜对了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多