Android中TextToSpeech的簡單使用

來源:互聯網
上載者:User

標籤:android開發   語言朗讀   texttospeech   

android也可以實現把輸入的文字朗讀出來,使用到的是TextToSpeech,不過目前只支援5種語言:English、 French 、 German 、 Italian 和 Spanish。(真遺憾,沒有Chinese),對android系統要求為android 1.6(API8)以上:

public class SpeechTestActivity extends Activity {

/**TextToSpeech對象*/
private TextToSpeech mText2Speech;
/**確定按鈕*/
private Button mBtn;
/**文本輸入框*/
private EditText mEdt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
initListeners();
mBtn = (Button) findViewById(R.id.test_btn);
mEdt = (EditText) findViewById(R.id.test_edt);
mBtn.setEnabled(false);


}


private void initListeners() {
mText2Speech = new TextToSpeech(this, new OnInitListener() {

@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {/**如果裝載TTS成功*/
int result = mText2Speech.setLanguage(Locale.ENGLISH);/**有Locale.CHINESE,但是不支援中文*/
if (result == TextToSpeech.LANG_MISSING_DATA/**表示語言的資料丟失。*/
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {/**語言不支援*/
Toast.makeText(SpeechTestActivity.this, "我說不出口", Toast.LENGTH_SHORT).show();
} else {
mBtn.setEnabled(true);
mText2Speech.speak("I miss you", TextToSpeech.QUEUE_FLUSH,
null);
}
}
}
});

mBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mText2Speech.speak(mEdt.getText().toString(),
TextToSpeech.QUEUE_FLUSH, null);
}
});
}


private void findViewsById() {
mBtn = (Button) findViewById(R.id.test_btn);
mEdt = (EditText) findViewById(R.id.test_edt);
}


@Override
protected void onDestroy() {
if (mText2Speech != null) {
mText2Speech.stop();
mText2Speech.shutdown();
}
super.onDestroy();
}
}

Android中TextToSpeech的簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.