Android TextToSpeech教程含代碼

    Android TextToSpeech教程

    在Android中,你可以通過 TextToSpeech 類將文本轉換為語音。愛掏網 - it200.com完成轉換后,你可以播放或創建聲音文件。愛掏網 - it200.com

    TextToSpeech類的構造方法

    • TextToSpeech(Context context, TextToSpeech.OnInitListener)

    TextToSpeech類的方法

    TextToSpeech類常用的方法如下:

    方法 描述
    **int speak (String text, int queueMode, HashMap params) ** 將文本轉換為語音。愛掏網 - it200.com隊列模式可以是QUEUE_ADD或QUEUE_FLUSH。愛掏網 - it200.com請求參數可以為空,也可以是KEY_PARAM_STREAM,KEY_PARAM_VALUME等。愛掏網 - it200.com
    int setSpeechRate(float speed) 設置語音的速度。愛掏網 - it200.com
    int setPitch(float speed) 設置語音的音調。愛掏網 - it200.com
    int setLanguage (Locale loc) 設置語音的具體區域特定語言。愛掏網 - it200.com
    void shutdown() 釋放由TextToSpeech Engine設置的資源。愛掏網 - it200.com
    int stop() 中斷當前的語音輸出(無論是播放還是保存到文件),并丟棄隊列中的其他語音輸出。愛掏網 - it200.com

    HTML格式的英文文本翻譯成中文保留HTML格式不變輸出

    您需要實現TextToSpeech.OnInitListener接口,以執行TextToSpeech引擎上的事件處理。愛掏網 - it200.com

    TextToSpeech.OnInitListener接口的方法

    此接口中只有一個方法。愛掏網 - it200.com

    方法 描述
    void onInit (int status) 在TextToSpeech引擎初始化完成后調用。愛掏網 - it200.com狀態可以是SUCCESS或ERROR。愛掏網 - it200.com

    Android TextToSpeech 示例

    讓我們編寫代碼將文本轉換為語音。愛掏網 - it200.com

    activity_main.xml

    在布局中拖動一個文本視圖,一個編輯框和一個按鈕。愛掏網 - it200.com現在activity_main.xml文件將會是這樣的:

    <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="77dp"
            android:layout_marginTop="42dp"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText1"
            android:layout_below="@+id/editText1"
            android:layout_marginLeft="59dp"
            android:layout_marginTop="39dp"
            android:text="Speak" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/editText1"
            android:layout_alignBottom="@+id/editText1"
            android:layout_alignParentLeft="true"
            android:text="Enter Text:" />
    
    </RelativeLayout>
    

    Activity類

    讓我們來看看如何編寫代碼來朗讀給定的文本。愛掏網 - it200.com

    package com.example.texttospeech;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    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.Button;
    import android.widget.EditText;
    public class MainActivity extends Activity implements
    TextToSpeech.OnInitListener {
    /** Called when the activity is first created. */
    
    private TextToSpeech tts;
    private Button buttonSpeak;
    private EditText editText;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    tts = new TextToSpeech(this, this);
    buttonSpeak = (Button) findViewById(R.id.button1);
    editText = (EditText) findViewById(R.id.editText1);
    
    buttonSpeak.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            speakOut();
        }
    
    });
    }
    
    @Override
    public void onDestroy() {
    // Don't forget to shutdown tts!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
    }
    
    @Override
    public void onInit(int status) {
    
    if (status == TextToSpeech.SUCCESS) {
    
        int result = tts.setLanguage(Locale.US);
    
        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
            buttonSpeak.setEnabled(true);
            speakOut();
        }
    
    } else {
        Log.e("TTS", "Initilization Failed!");
    }
    
    }
    
    private void speakOut() {
    String text = editText.getText().toString();
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    
    

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 日韩视频在线观看一区二区| 清纯唯美经典一区二区| 国产精品av一区二区三区不卡蜜 | 99久久精品日本一区二区免费| 国产精品免费视频一区| 亚洲线精品一区二区三区影音先锋| 亚洲国产精品一区二区成人片国内| 精品亚洲福利一区二区| 国产精品毛片VA一区二区三区 | 国产精品区一区二区三| 亚洲综合色一区二区三区| 无码日韩精品一区二区免费暖暖| 好爽毛片一区二区三区四| 美女视频黄a视频全免费网站一区| 91精品一区二区综合在线| 亚洲成AV人片一区二区| 伊人久久大香线蕉av一区| 久久精品一区二区影院| 精品一区二区三区免费观看| 国产一区中文字幕在线观看| 无码一区二区三区亚洲人妻| 日韩aⅴ人妻无码一区二区| 国产乱码精品一区二区三区四川人| 精品无码人妻一区二区免费蜜桃| 无码精品黑人一区二区三区| 亚洲A∨无码一区二区三区| 久久久精品一区二区三区| 看电影来5566一区.二区| 午夜福利av无码一区二区| 久久se精品一区二区国产| 亚洲高清美女一区二区三区| 精品欧洲av无码一区二区| 日本激情一区二区三区| 国产麻豆精品一区二区三区| 天堂va视频一区二区| 免费视频精品一区二区| 国产伦精品一区二区三区视频猫咪| 久久免费视频一区| 无码人妻一区二区三区在线| 麻豆天美国产一区在线播放| 大香伊人久久精品一区二区 |