package com.skex.ttsdemo

来源:互联网 发布:指导生活的算法mobi 编辑:程序博客网 时间:2024/05/21 11:02

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <EditText
        android:id="@+id/input_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10" >


        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/speak_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/input_text"
        android:layout_below="@+id/input_text"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="36dp"
        android:text="ReadText" />


</RelativeLayout>




package com.skex.ttsdemo;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity implements OnInitListener{


    private int MY_DATA_CHECK_CODE = 0;


    private TextToSpeech tts;


    private EditText inputText;
    private Button speakButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputText = (EditText) findViewById(R.id.input_text);
        speakButton = (Button) findViewById(R.id.speak_button);


        speakButton.setOnClickListener(new OnClickListener() {            
        @Override
        public void onClick(View v) {
        String text = inputText.getText().toString();
        if (text!=null && text.length()>0) {
      Toast.makeText(MainActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
      tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        }
        }
            });


        Intent checkIntent = new Intent();
            checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
            startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);


      }




      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (requestCode == MY_DATA_CHECK_CODE) {
                  if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                      // success, create the TTS instance
                      tts = new TextToSpeech(this, this);
                  } 
                  else {
                      // missing data, install it
                      Intent installIntent = new Intent();
                      installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                      startActivity(installIntent);
                  }
              }


          }


          @Override
          public void onInit(int status) {        
              if (status == TextToSpeech.SUCCESS) {
                  Toast.makeText(MainActivity.this, 
                          "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
              }
              else if (status == TextToSpeech.ERROR) {
                  Toast.makeText(MainActivity.this, 
                          "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
              }
          }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}



0 0
原创粉丝点击