Text to speech(TTS)-Android

前端 未结 7 985
星月不相逢
星月不相逢 2020-11-27 11:42

I am new to the android platform. Now I am working on TTS(Text to Speech).If I enter the text in a TextArea and I would like it to be converted to speech when i

7条回答
  •  情深已故
    2020-11-27 11:51

    public class Texttovoice extends ActionBarActivity implements OnInitListener {
    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texttovoice);
        tts = new TextToSpeech(this, this);
    
        // Refer 'Speak' button
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        // Refer 'Text' control
        txtText = (EditText) findViewById(R.id.txtText);
        // Handle onClick event for button 'Speak'
        btnSpeak.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0) {
                // Method yet to be defined
                speakOut();
            }
    
        });
    
    }
    
    private void speakOut() {
        // Get the text typed
        String text = txtText.getText().toString();
        // If no text is typed, tts will read out 'You haven't typed text'
        // else it reads out the text you typed
        if (text.length() == 0) {
            tts.speak("You haven't typed text", TextToSpeech.QUEUE_FLUSH, null);
        } else {
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    
        }
    
    }
    
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.texttovoice, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public void onInit(int status) {
        // TODO Auto-generated method stub
        // TTS is successfully initialized
        if (status == TextToSpeech.SUCCESS) {
            // Setting speech language
            int result = tts.setLanguage(Locale.US);
            // If your device doesn't support language you set above
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                // Cook simple toast message with message
                Toast.makeText(getApplicationContext(), "Language not supported",
                        Toast.LENGTH_LONG).show();
                Log.e("TTS", "Language is not supported");
            }
            // Enable the button - It was disabled in main.xml (Go back and
            // Check it)
            else {
                btnSpeak.setEnabled(true);
            }
            // TTS is not initialized properly
        } else {
            Toast.makeText(this, "TTS Initilization Failed", Toast.LENGTH_LONG)
                    .show();
            Log.e("TTS", "Initilization Failed");
        }
    }
       //-------------------------------XML---------------
    
      
     
    
        
    
    
    
    

提交回复
热议问题