自动朗读 TTS

public class MainActivity extends Activity {

    TextToSpeech tts;
    EditText say;
    Button speech,record;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tts=new TextToSpeech(this, new OnInitListener() {
            
            @Override
            public void onInit(int status) {
                if(status==TextToSpeech.SUCCESS)
                {
                    int result=tts.setLanguage(Locale.getDefault());
                    if(result!=TextToSpeech.LANG_COUNTRY_AVAILABLE&&result!=TextToSpeech.LANG_AVAILABLE)
                    {
                        Toast.makeText(MainActivity.this, "TTS暂时不支持这种语言的朗读", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        findid();
    }

    private void findid() {
        // TODO Auto-generated method stub
        say=(EditText) findViewById(R.id.say);
        speech=(Button) findViewById(R.id.speech);
        record=(Button) findViewById(R.id.record);
        speech.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                tts.speak(say.getText().toString(), TextToSpeech.QUEUE_ADD, null);
            }
        });
        record.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                tts.synthesizeToFile(say.getText().toString(), null, "/mnt/sdcard/sound.wav");
                Toast.makeText(MainActivity.this, "声音记录成功!", Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(tts!=null)
        {
            tts.shutdown();
        }
    }

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

}

posted @ 2013-10-02 15:38  萨拉克魔  阅读(264)  评论(1编辑  收藏  举报