android 朗读英文,Android中用TTS语音朗读一段英文
Android中自带的TTS语音服务,现在不能读取汉字,下面是我用来读取一段英文的代码,希望对大家有帮助:package com.yu;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.s
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();
}
}
}
更多推荐
所有评论(0)