当前位置: 代码迷 >> Android >> android关于 text_to_speach的施用(tts)
  详细解决方案

android关于 text_to_speach的施用(tts)

热度:85   发布时间:2016-05-01 15:35:38.0
android关于 text_to_speach的使用(tts)

主要用到两个包

import android.speech.tts.TextToSpeech;

import android.speech.tts.TextToSpeech.OnInitListener;

其中必须实现OnInitListener里面的方法

@Override	public void onInit(int status) {				if(status != TextToSpeech.SUCCESS)		{			Log.i("TTS_TEST", "TTS engine failed to initialize");			finish();		}		else		{			Log.i("TTS_TEST", "tts engine initialized");			//Do nothing special?		}	}

?

然后我们就实现 一个文本框输入? 要听的文字 一个是button按钮,只要按住button按钮就可以听到里面的英语了

这里面我们没有自己进行设置,都是默认设置 当然你可以在模拟机的setting设置

?

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

?

然后就开是设置一个activity,来检测设备安装的tts

protected void onActivityResult(int reqCode, int resCode, Intent data)    {    	if(reqCode == CHECK_TTS_RES) //Result from checkIntent in onCreate    	{    		if(resCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)    		{    			//Data is good to go!  Initialize the TTS instance    			 Log.i("TTS_TEST", "onActivityResult complete");    			tts = new TextToSpeech(this, this);    		}    		else    		{    			//Fail!  Let the user install the data    			Intent installTTSDataIntent = new Intent();    			installTTSDataIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);    			startActivity(installTTSDataIntent);    		}    	}    }

?

如果有合适的资源 那么就实例化一个tts 传给button,button就通过这个实例调用speak方法就可以了

 OnClickListener mSubmitListener = new OnClickListener() {				public void onClick(View v) {			//Log the second parameter with the first parameter as the tag.			//Note: Window -> Show View... -> Other -> LogCat to view the log in Eclipse			Log.i("TTS_TEST", "Button clicked - Attempting to speak");						//On button click, say whatever is in the textField			tts.speak(vText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);		}	};

?

TextToSpeech.Engine.ACTION_CHECK_TTS_DATA

这里一定要注意TextToSpeech.Engine下的几个常量。

源码来源于网络

  相关解决方案