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下的几个常量。

源码来源于网络

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值