`
梦想家dream
  • 浏览: 61686 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

Android TextToSpeech语音播放文本

阅读更多
步骤一、初始化
package com.example.speakdemo2;

import java.util.Locale;

import android.app.Activity;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Help {
	private Activity activity;
	private TextToSpeech textToSpeech;

	public Help(Activity activity) {
		this.activity = activity;
		textToSpeech = new TextToSpeech(activity,
				new TextToSpeech.OnInitListener() {

					@Override
					public void onInit(int status) {
						if (status == TextToSpeech.SUCCESS) {
							int result = textToSpeech.setLanguage(Locale.US);
							if (result == TextToSpeech.LANG_MISSING_DATA
									|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
								Log.e("MESSAGE", "不支持的语言");
							}

						}

					}
				});

	}

	public void speak(String voice) {
		textToSpeech.speak(voice, TextToSpeech.QUEUE_FLUSH, null);
	}

}


步骤二、文本转语音播放
package com.example.speakdemo2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	Help help;
	EditText editText;
	Button button;

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

		help = new Help(this);

		editText = (EditText) findViewById(R.id.edit_text);
		button = (Button) findViewById(R.id.btn);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				help.speak(editText.getText().toString());

			}
		});

	}

}


源码下载请点这里:
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics