Android實戰--小DEMO(JAVA關鍵字學習之語音合成TTS的實現)一

來源:互聯網
上載者:User

標籤:android   tts   

接著上一節討論的問題,本DEMO中會用到TTS語音合成,我們下面介紹一個同樣原理的小例子

看一下布局檔案很簡單:

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:background="@android:color/white"   >  <TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"  />  <EditText     android:id="@+id/EditText01"     android:layout_width="wrap_content"     android:layout_height="wrap_content"    android:hint="Input something to speak..."  >  </EditText>  <ImageButton     android:id="@+id/ImageButton01"     android:layout_width="wrap_content"     android:layout_height="wrap_content"    android:src="@android:drawable/ic_btn_speak_now"  >  </ImageButton></LinearLayout>


下面是Activity:

package irdc.ex07_18;import java.util.Locale;import android.app.Activity;import android.os.Bundle;import android.speech.tts.TextToSpeech;import android.util.Log;import android.view.View;import android.widget.ImageButton;import android.widget.EditText;public class EX07_18 extends Activity{  public static String TAG = "EX07_18_DEBUG";  private TextToSpeech tts;  private EditText EditText01;  private ImageButton ImageButton01;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    /* 傳入context及OnInitListener */    tts = new TextToSpeech(this, ttsInitListener);    EditText01 = (EditText) this.findViewById(R.id.EditText01);    ImageButton01 = (ImageButton) this.findViewById(R.id.ImageButton01);    ImageButton01.setOnClickListener(new ImageButton.OnClickListener()    {      @Override      public void onClick(View v)      {        // TODO Auto-generated method stub        if (EditText01.getText().length() > 0)        {          /* 傳入要說的字串 */          tts.speak(EditText01.getText().toString(), TextToSpeech.QUEUE_FLUSH,              null);        } else        {          /* 無輸入字串時 */          tts.speak("Nothing to say", TextToSpeech.QUEUE_FLUSH, null);        }      }    });  }  private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener()  {    @Override    public void onInit(int status)    {      // TODO Auto-generated method stub      /* 使用美國時區目前不支援中文 */      Locale loc = new Locale("us", "", "");      /* 檢查是否支援輸入的時區 */      if (tts.isLanguageAvailable(loc) == TextToSpeech.LANG_AVAILABLE)      {        /* 設定語言 */        tts.setLanguage(loc);      }      tts.setOnUtteranceCompletedListener(ttsUtteranceCompletedListener);      Log.i(TAG, "TextToSpeech.OnInitListener");    }  };  private TextToSpeech.OnUtteranceCompletedListener ttsUtteranceCompletedListener = new TextToSpeech.OnUtteranceCompletedListener()  {    @Override    public void onUtteranceCompleted(String utteranceId)    {      // TODO Auto-generated method stub      Log.i(TAG, "TextToSpeech.OnUtteranceCompletedListener");    }  };  @Override  protected void onDestroy()  {    // TODO Auto-generated method stub    /* 釋放TextToSpeech的資源 */    tts.shutdown();    Log.i(TAG, "tts.shutdown");    super.onDestroy();  }}


運行執行個體效果:

TTS瞭解了,具體可以用到哪些地方為你的應用添彩,就看你個人的創意了。

源碼下載:http://download.csdn.net/detail/yayun0516/8710403

應用:

                                       http://openbox.mobilem.360.cn/index/d/sid/2966005

                                       http://android.myapp.com/myapp/detail.htm?apkName=com.yayun.gitlearning

歡迎下載,有問題多交流!(喜歡的請關注我,謝謝!)

 

 

Android實戰--小DEMO(JAVA關鍵字學習之語音合成TTS的實現)一

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.