Android的TTS语音合成器 [英] Android TTS speech synthesizer

查看:159
本文介绍了Android的TTS语音合成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有什么办法可以将输入保存到Android speaker.I的意思是保存当前的呼声正在播放由我所需要的speaker.This由于Android的TTS语音合成器不work.So我不能保存声音,我就可以发挥它。 多谢你, Sreekanth

I want to know whether is there any way I can save the input to the android speaker.I mean save the current voice being played by the speaker.This I need because android TTS speech synthesizer doesn't work.So I cannot save the audio,I can just play it. Thanking you, Sreekanth

推荐答案

通过以下code解决的问题(机器人TTS开始工作)

Solved the issue by the following code (android TTS started working)

public class TextToSpeechActivity extends Activity implements OnInitListener {
  private static TextToSpeech mTts;
  private int MY_DATA_CHECK_CODE    =   0;
  private Button mSpeakButton;
 //String name;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSpeakButton  = (Button)findViewById(R.id.btnSpeak);        
        mSpeakButton.setEnabled(false);
        mSpeakButton.setOnClickListener(new OnClickListener() {  
           @Override
           public void onClick(View v) {
            sayHello();
           }
        });

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

    }


    public void onInit(int status){
  // TODO Auto-generated method stub
  if (status == TextToSpeech.SUCCESS) {
             // Set preferred language to US english.
            // Note that a language may not be available, and the result will indicate this.
            int result = mTts.setLanguage(Locale.US);
            // Try this someday for some interesting results.
            // int result mTts.setLanguage(Locale.FRANCE);
            if (result == TextToSpeech.LANG_MISSING_DATA ||
                result == TextToSpeech.LANG_NOT_SUPPORTED) {
               // Lanuage data is missing or the language is not supported.
                Log.e("TAG", "Language is not available.");
            }
            else 
            {
                // Check the documentation for other possible result codes.
                // For example, the language may be available for the locale,
                // but not for the specified country and variant.

                // The TTS engine has been successfully initialized.
            }
        } else {
            // Initialization failed.
            Log.e("TAG", "Could not initialize TextToSpeech.");
        }

 }

    private void sayHello() 
    {        
        String speakTextTxt                  = "Good morning,have a nice day";
        HashMap<String, String> myHashRender = new HashMap<String, String>();
        myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, speakTextTxt);

        String exStoragePath                = Environment.getExternalStorageDirectory().getAbsolutePath();
        File appTmpPath                     = new File(exStoragePath);
        appTmpPath.mkdirs();
        String tempFilename                 = "tmpaudio.wav";
        String tempDestFile                 = appTmpPath.getAbsolutePath() + "/" + tempFilename;

        mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile);
    }

 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
      super.onDestroy();
         if (mTts != null) {
             mTts.stop();
             mTts.shutdown();
          }

 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == MY_DATA_CHECK_CODE) {
           if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { //the user has the necessary data - create the TTS
               mTts  =  new TextToSpeech(this, this);
               mSpeakButton.setEnabled(true);

           }else {               //no data - install it now will no go to Google Play.
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
           }
      }
 }
}

这篇关于Android的TTS语音合成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆