找传奇、传世资源到传世资源站!

AndroidTTSDemo_speak_文字识别并朗读__

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

AndroidTTSDemo_speak_文字识别并朗读__ Android平台开发-第1张

package com.studio;

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

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

public class AndroidTTSDemo 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 complete me. You had me at hello, 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>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		inputText = (EditText) findViewById(R.id.inputText);
		inputText.setText(loveConfession);
		speakBtn = (Button) findViewById(R.id.speakBtn);
		mTts = new TextToSpeech(this, this);
		
		SharedPreferences settings = getSharedPreferences(STORE_NAME, 0);
		lastUtterance = settings.getInt("lastUtterance", -1);
		Log.v(TAG, "the restored lastUtterance is "  lastUtterance);
		
		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() 
			{
				// TODO Auto-generated method stub
				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
		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
	protected void onDestroy() 
	{
		// TODO Auto-generated method stub
		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();
	}

	@Override
	public void onUtteranceCompleted(String utteranceId) 
	{
		// TODO Auto-generated method stub
		Log.v(TAG, "Get completed message for the utteranceId "  utteranceId);
		lastUtterance = Integer.parseInt(utteranceId);
	}
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复