Android中用TTS語音朗讀一段英文

來源:互聯網
上載者:User

     Android中內建的TTSVoice Messaging Service,現在不能讀取漢字,下面是我用來讀取一段英文的代碼,希望對大家有協助:

package com.yu;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.widget.Button;import android.widget.EditText;public class TTSTestActivity extends Activity {Button  button;EditText  edittext;TextToSpeech mSpeech;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        edittext=(EditText)findViewById(R.id.edittext);        button=(Button)findViewById(R.id.button);        button.setEnabled(false);        mSpeech = new TextToSpeech(this, new OnInitListener()         {            @Override            public void onInit(int status)            {                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                     {//類比機在啟動時朗讀下面的英文                        button.setEnabled(true);                        mSpeech.speak("Hello World,Hello Android", TextToSpeech.QUEUE_FLUSH, null);                    }                }            }        });        button.setOnClickListener(new Button.OnClickListener()         {            @Override            public void onClick(View v)             {//讀取文字框裡輸入的英文                mSpeech.speak(edittext.getText().toString(),TextToSpeech.QUEUE_FLUSH, null);            }        });    }@Overrideprotected void onDestroy() {super.onDestroy();if (mSpeech != null) {            mSpeech.stop();            mSpeech.shutdown();        }}}

聯繫我們

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