0

我正在使用TextToSpeech類來使系統發言。 首先,我通過語音識別器說話,並將文本設置爲textView。接下來,我希望系統能夠說出我之前發言的內容。 我發現第一步工作成功,但我說話後我什麼也沒聽到。來自識別結果的文本到語音

我的代碼如下:

public class MainActivity extends AppCompatActivity { 

    private int REQUEST_CODE=1; 
    TextView text; 
    ImageButton button; 
    TextToSpeech textToSpeech; 
    private Button ttsButton; 
    private ArrayList<String> results; 
    private String s; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 


      button=(ImageButton)findViewById(R.id.imageButton); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
       intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
         RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 

       startActivityForResult(intent, REQUEST_CODE); 


        } 
     }); 

     textToSpeech =new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() { 
      @Override 
      public void onInit(int status) { 
       if(status==TextToSpeech.SUCCESS) 
        textToSpeech.setLanguage(Locale.US); 

      } 
     }); 
     textToSpeech.speak(s, TextToSpeech.QUEUE_FLUSH, null,null); 


    } 

    public void onPause(){ 
     if(textToSpeech !=null){ 
      textToSpeech.stop(); 
      textToSpeech.shutdown(); 
     } 
     super.onPause(); 
    } 



    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 
      results = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS); 
      s=results.get(0); 
      text=(TextView)findViewById(R.id.text); 
      text.setText(""+results.get(0)); 


     } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 

} 

回答

1

你忘了活動結果後觸發TTS引擎: -

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 
      results = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS); 
      s=results.get(0); 
      text=(TextView)findViewById(R.id.text); 
      text.setText(""+results.get(0)); 

      //Trigger text to speech here 
      textToSpeech.speak(results.get(0), TextToSpeech.QUEUE_FLUSH, null,null); 

     } 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
+0

添加缺少的行.still它不工作 – Raman

+2

也發表評論的onPause你停止每次打開語音輸入對話框時,都會調用onPause和onPuse中的TTS引擎。 –