首页>代码>android TTS 语音,利用系统自带的API来语音提示,不支持中文>/AndroidTTSDemoThird/src/com/example/androidttsdemothird/MainActivity.java
package com.example.androidttsdemothird;

import java.util.HashMap;
import java.util.Locale;
import java.util.StringTokenizer;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnInitListener,
		OnUtteranceCompletedListener {

	private EditText inputText = null;
	private Button speakBtn = null;
	private TextToSpeech mTts;
	private static final String TAG = "TTS Demo";
	private static final String STORE_NAME = "preferenceFile";
	private static final String loveConfession = "你好, you had me at hello. To the world you maybe one person, but to one person you maybe the world. Please believe me, I was prepared for everything,except you. I love that you are the last person I want to talk to before I go to sleep at night. Love means never having to say you're sorry. So choose me. Marry me. Let me make you happy. ";
	private String[] loveArray;
	private int lastUtterance = -1;
	private HashMap<String, String> params = new HashMap<String, String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 创建TextToSpeech实例,初始化完成后会调用OnInitListener(第二个参数)的回调函数
		mTts = new TextToSpeech(this, this // TextToSpeech.OnInitListener
		);
		
		//设置控件
        inputText = (EditText)findViewById(R.id.inputText);
        speakBtn = (Button)findViewById(R.id.speakBtn);
       
	    inputText.setText(loveConfession);  
        speakBtn.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//处理输入框里的内容
				StringTokenizer loveTokens = new StringTokenizer(inputText.getText().toString(),",.");
				int i = 0;
				loveArray = new String[loveTokens.countTokens()];
				while(loveTokens.hasMoreTokens())
				{
					loveArray[i++] = loveTokens.nextToken();
				}
				//朗读输入框里的内容
				speakText();
			}
		});
	}

	private void speakText() {
		lastUtterance++;
		if(lastUtterance >= loveArray.length)
		{
			lastUtterance = 0;
		}
		Log.v(TAG, "the begin utterance is " + lastUtterance);
		for(int i = lastUtterance; i < loveArray.length; i++)
		{
			//为每个语音片段都设置一个标记
			params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.valueOf(i));
			mTts.speak(loveArray[i], TextToSpeech.QUEUE_ADD, params);
		}
	}

	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
		//TTS Engine初始化完成
		if(status == TextToSpeech.SUCCESS)
		{
			int result = mTts.setLanguage(Locale.US);
			//设置发音语言
			if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
			//判断语言是否可用
			{
				Log.v(TAG, "Language is not available");
				speakBtn.setEnabled(false);
			}
			else
			{
				speakBtn.setEnabled(true);
				//设置一个语音片段结束后的回调函数
				mTts.setOnUtteranceCompletedListener(this);
			}
		}	
	}

	@Override
	public void onUtteranceCompleted(String utteranceId) {
		// TODO Auto-generated method stub
		//一个语音片段结束后的回调函数
		Log.v(TAG, "Get completed message for the utteranceId " + utteranceId);
		//用lastUtterance记录当前结束的语音片段
		lastUtterance = Integer.parseInt(utteranceId);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		//释放TTS的资源
		if(mTts != null)
		{
			mTts.stop();
			mTts.shutdown();
		}
		//保存lastUtterance值
		SharedPreferences settings = getSharedPreferences(STORE_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("lastUtterance", lastUtterance);
		editor.commit();
		Log.v(TAG, "the stored lastUtterance is " + lastUtterance);
		
		super.onDestroy();
	}

}
最近下载更多
lilekai  LV1 2022年6月30日
lizhoutao  LV11 2020年12月11日
1376662442  LV1 2020年4月19日
15980264570  LV1 2020年3月10日
lwmnhm  LV8 2019年5月10日
辉辉辉辉辉辉  LV10 2019年2月21日
YanNianJunZi  LV3 2018年4月25日
893659297  LV1 2018年1月4日
没桨的船  LV1 2017年12月17日
swxr33  LV2 2016年11月7日
最近浏览更多
d7andywei  LV1 2023年1月19日
yohohero  LV1 2023年1月15日
uni-code_0123  LV1 2022年11月16日
yuandian  LV1 2022年8月8日
lilekai  LV1 2022年6月30日
361424231  LV4 2022年6月16日
cxywt12  LV2 2022年5月20日
xtaliang  LV1 2022年3月28日
软弱中的坚强  LV1 2022年3月14日
zhenghongwei  LV2 2022年3月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友