zoukankan      html  css  js  c++  java
  • 自动朗读 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;
        }

    }

  • 相关阅读:
    TcxComboBoxProperties下拉框填充
    Delphi用QJSON解析JSON格式的数据 【转】
    Delphi的idhttp报IOHandler value is not valid错误的原因[转]
    RelayCommand
    ViewModelBase && ObservableObject
    MvvmLight ToolKit 教程
    WPF中的数据验证
    MvvmLight ToolKit .Net4.5版本 CanExecute不能刷新界面bug
    微软虚拟学院MVA 字幕获取方法
    WPF Binding INotifyPropertyChanged 多线程 深入理解
  • 原文地址:https://www.cnblogs.com/lk119/p/3349372.html
Copyright © 2011-2022 走看看