Android中自带的TTS语音服务,现在不能读取汉字,下面是我用来读取一段英文的代码,希望对大家有帮助:

package com.yu;

import java.util.Locale;

import android.app.Activity;

import android.os.Bundle;

import android.speech.tts.TextToSpeech;

import android.speech.tts.TextToSpeech.OnInitListener;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

public class TTSTestActivity extends Activity

{

Button button;

EditText edittext;

TextToSpeech mSpeech;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

edittext=(EditText)findViewById(R.id.edittext);

button=(Button)findViewById(R.id.button);

button.setEnabled(false);

mSpeech = new TextToSpeech(this, new OnInitListener()

{

@Override

public void onInit(int status)

{

if (status == TextToSpeech.SUCCESS)

{

int result = mSpeech.setLanguage(Locale.ENGLISH);//设置只能朗读英文

if (result == TextToSpeech.LANG_MISSING_DATA|| result == TextToSpeech.LANG_NOT_SUPPORTED)

{//要是结果没值,就在后台打印出来

Log.e("lanageTag", "not use");

}

else

{//模拟机在启动时朗读下面的英文

button.setEnabled(true);

mSpeech.speak("Hello World,Hello Android", TextToSpeech.QUEUE_FLUSH, null);

}

}

}

});

button.setOnClickListener(new Button.OnClickListener()

{

@Override

public void onClick(View v)

{//读取文本框里输入的英文

mSpeech.speak(edittext.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);

}

});

}

@Override

protected void onDestroy()

{

super.onDestroy();

if (mSpeech != null)

{

mSpeech.stop();

mSpeech.shutdown();

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐