Android 輕松實(shí)現(xiàn)語(yǔ)音朗讀
?
轉(zhuǎn)載自 http://terryblog.blog.51cto.com/1764499/373812
?
原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章 原始出處 、作者信息和本聲明。否則將追究法律責(zé)任。 http://terryblog.blog.51cto.com/1764499/373812
?
?
語(yǔ)音朗讀,這是一個(gè)很好的功能,可以實(shí)現(xiàn)一些客戶的特殊要求。在Android 實(shí)現(xiàn)主意功能只需要幾段簡(jiǎn)單的代碼即可完成。
?
在Android 中使用語(yǔ)音朗讀功能 只需要使用此類 TextToSpeech ,該類實(shí)現(xiàn)了很多關(guān)于語(yǔ)音的功能,使用該類必須為其設(shè)置語(yǔ)言,支持語(yǔ)言列表位于java.util類里的Local 類,具體如下:
?
屏幕問題,顯示不足,大家可以去SDK查看。雖然支持眾多語(yǔ)言列表,可是貌似Android 內(nèi)置語(yǔ)音朗讀的語(yǔ)言種類并不多,是不是以后得在寫系統(tǒng)的時(shí)候編進(jìn)去還是怎么樣,這個(gè)不知所以然,目前我只測(cè)試了English 和 Chinese。 English 是可行的,Chinese 失敗了。OK ,廢話不多說(shuō), 上全部實(shí)現(xiàn)代碼:
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(); } }
?
?
代碼簡(jiǎn)單明了,不做過多介紹。
本文出自 “ Terry_龍 ” 博客,請(qǐng)務(wù)必保留此出處 http://terryblog.blog.51cto.com/1764499/373812
?
?
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
