Android 輕松實現語音朗讀
?
轉載自 http://terryblog.blog.51cto.com/1764499/373812
?
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。 http://terryblog.blog.51cto.com/1764499/373812
?
?
語音朗讀,這是一個很好的功能,可以實現一些客戶的特殊要求。在Android 實現主意功能只需要幾段簡單的代碼即可完成。
?
在Android 中使用語音朗讀功能 只需要使用此類 TextToSpeech ,該類實現了很多關于語音的功能,使用該類必須為其設置語言,支持語言列表位于java.util類里的Local 類,具體如下:
?
屏幕問題,顯示不足,大家可以去SDK查看。雖然支持眾多語言列表,可是貌似Android 內置語音朗讀的語言種類并不多,是不是以后得在寫系統的時候編進去還是怎么樣,這個不知所以然,目前我只測試了English 和 Chinese。 English 是可行的,Chinese 失敗了。OK ,廢話不多說, 上全部實現代碼:
package com.terry;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class speechActivity extends Activity {
private TextToSpeech mSpeech;
private Button btn;
private EditText mEditText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
mEditText = (EditText) findViewById(R.id.EditText01);
btn.setEnabled(false);
mSpeech = new TextToSpeech(this, new OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
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 {
btn.setEnabled(true);
mSpeech.speak("i love you", TextToSpeech.QUEUE_FLUSH,
null);
}
}
}
});
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mSpeech.speak(mEditText.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if (mSpeech != null) {
mSpeech.stop();
mSpeech.shutdown();
}
super.onDestroy();
}
}
?
?
代碼簡單明了,不做過多介紹。
本文出自 “ Terry_龍 ” 博客,請務必保留此出處 http://terryblog.blog.51cto.com/1764499/373812
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

