目前好像沒有支援中文

有簡易版跟進階版 咖啡色為重點程式碼

簡易版就是

implements OnInitListener

並Override OnInit就可以使用Speak

================================================

public class AndroidSpeech extends Activity implements OnInitListener{

TextToSpeech TTS;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TTS = new TextToSpeech(this, this);

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(buttonOnClickListener);
}

private Button.OnClickListener buttonOnClickListener =
new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
String textAndroid = "Android";
TTS.speak(textAndroid, TextToSpeech.QUEUE_ADD, null);
}

};

public void onInit(int arg0) {
// TODO Auto-generated method stub

}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
TTS.shutdown();
}

}

==============================================================================

進階版

則是不用implements OnInitListener
但是要Override

TextToSpeech.OnInitListener

TextToSpeech.OnUtteranceCompletedListener

 


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();
  }


}


文章標籤
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()