Android 輕鬆實現語音朗讀

來源:互聯網
上載者:User

標籤:shutdown   not   body   created   layout   span   code   create   bsp   

語音朗讀,這是一個很好的功能,可以實現一些客戶的特殊要求。在Android 實現主意功能只需要幾段簡單的代碼即可完成。

 

 

在Android 中使用語音朗讀功能 只需要使用此類 TextToSpeech ,該類實現了很多關於語音的功能,使用該類必須為其設定語言,支援語言列表位於java.util類裡的Local 類,具體如下:

螢幕問題,顯示不足,大家可以去SDK查看。雖然支援眾多主意列表,可是貌似Android 內建語音朗讀的語言種類並不多,是不是以後得在寫系統的時候編進去還是怎麼樣,這個不知所以然,目前我只測試了English 和 Chinese。 English 是可行的,Chinese 失敗了。OK ,廢話不多說, 上全部實現代碼:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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();    }}

 

Android 輕鬆實現語音朗讀

相關文章

聯繫我們

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