欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)自動朗讀功能(TTS)

 更新時間:2021年09月10日 11:40:12   作者:tracydragonlxy  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)自動朗讀功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言: Android提供了自動朗讀支持??梢詫χ付ㄎ谋緝?nèi)容進(jìn)行朗讀,從而發(fā)生聲音;還允許把文本對應(yīng)的音頻錄制成音頻文件,方便以后播放。Android的自動朗讀主要通過TextToSpeech來完成,構(gòu)造器如:TextToSpeech(Context context, TextToSpeech.OnInitListennet listener);當(dāng)創(chuàng)建TextToSpeech對象時,必須先提供一個OnInitListener監(jiān)聽器——負(fù)責(zé)監(jiān)聽TextToSpeech的初始化結(jié)果。

效果圖如下:

使用TextToSpeech的步驟如下:

1、創(chuàng)建TextToSpeech對象,創(chuàng)建時傳入OnInitListener監(jiān)聽器監(jiān)聽示范創(chuàng)建成功。
2、設(shè)置TextToSpeech所使用語言國家選項,通過返回值判斷TTS是否支持該語言、國家選項。
3、調(diào)用speak()或synthesizeToFile方法。
4、關(guān)閉TTS,回收資源。

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/input_text"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/speech"
            android:text="Speech"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        <Button
            android:id="@+id/record"
            android:text="Record"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</LinearLayout>

Activity文件

public class SpeechActivity extends AppCompatActivity {

    private EditText input;
    private Button speech,record;

    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speech);

        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    int result = textToSpeech.setLanguage(Locale.CHINA);
                    if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                            && result != TextToSpeech.LANG_AVAILABLE){
                        Toast.makeText(SpeechActivity.this, "TTS暫時不支持這種語音的朗讀!",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        input = (EditText) findViewById(R.id.input_text);
        speech = (Button) findViewById(R.id.speech);
        record = (Button) findViewById(R.id.record);

        speech.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textToSpeech.speak(input.getText().toString(),
                        TextToSpeech.QUEUE_ADD, null);
            }
        });

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String inputText = input.getText().toString();
                HashMap<String, String> myHashRender = new HashMap<>();
                myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, inputText);
                textToSpeech.synthesizeToFile(inputText, myHashRender,
                        "/mnt/sdcard/my_recorder_audios/sound.wav");
                Toast.makeText(SpeechActivity.this, "聲音記錄成功。", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (textToSpeech != null)
            textToSpeech.shutdown();
        super.onDestroy();
    }
}

這里我們使用的是中文,int result = textToSpeech.setLanguage(Locale.CHINA);你也可以根據(jù)自己的需求更改為其他支持的語言。

最后在AndroidManifest.xml中加入權(quán)限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

總結(jié):通過使用Android提供的TTS,我們可以對指定文本內(nèi)容進(jìn)行朗讀,從而發(fā)生聲音;還允許把文本對應(yīng)的音頻錄制成音頻文件,保存到本地。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android 使用虛擬機安裝apk(圖文教程)

    android 使用虛擬機安裝apk(圖文教程)

    android 使用虛擬機安裝apk對一些新手朋友會很陌生,今天教大家使用使用虛擬機安裝apk文件,步驟很詳細(xì),有需要的朋友可以參考下
    2012-12-12
  • Android中使用ShareSDK集成分享功能的實例代碼

    Android中使用ShareSDK集成分享功能的實例代碼

    下面小編就為大家分享一篇Android中使用ShareSDK集成分享功能的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android編程實現(xiàn)自定義toast示例

    Android編程實現(xiàn)自定義toast示例

    這篇文章主要介紹了Android編程實現(xiàn)自定義toast,結(jié)合簡單實例形式分析了自定義布局toast核心實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2017-01-01
  • 簡單說說Android中如何使用攝像頭和相冊

    簡單說說Android中如何使用攝像頭和相冊

    本篇文章主要介紹了簡單說說Android中如何使用攝像頭和相冊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android幀式布局實現(xiàn)自動切換顏色

    Android幀式布局實現(xiàn)自動切換顏色

    這篇文章主要為大家詳細(xì)介紹了Android幀式布局實現(xiàn)自動切換顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android?App頁面滑動標(biāo)題欄顏色漸變詳解

    Android?App頁面滑動標(biāo)題欄顏色漸變詳解

    這篇文章主要為大家詳細(xì)介紹了Android?App頁面滑動標(biāo)題欄顏色漸變,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Flutter進(jìn)階之實現(xiàn)動畫效果(四)

    Flutter進(jìn)階之實現(xiàn)動畫效果(四)

    這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實現(xiàn)動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android動態(tài)加載布局實現(xiàn)技巧介紹

    Android動態(tài)加載布局實現(xiàn)技巧介紹

    通過使用LayoutInflater 每次點擊按鈕時候去讀取布局文件,然后找到布局文件里面的各個VIEW 操作完VIEW 后加載進(jìn)我們setContentView 方面里面的要放的布局文件里面,每次動態(tài)加載文件必需調(diào)用 removeAllViews方法,清除之前的加載進(jìn)來的View
    2022-12-12
  • Android利用FlexboxLayout輕松實現(xiàn)流動布局

    Android利用FlexboxLayout輕松實現(xiàn)流動布局

    flexbox是屬于CSS的一種布局方案,可以簡單、完整、響應(yīng)式的實現(xiàn)各種頁面布局。谷歌將其引入以提高復(fù)雜布局的能力。下面這篇文章主要給大家介紹了在Android中利用FlexboxLayout輕松實現(xiàn)流動布局的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • Android入門之Activity四種啟動模式(standard、singleTop、singleTask、singleInstance)

    Android入門之Activity四種啟動模式(standard、singleTop、singleTask、singl

    當(dāng)應(yīng)用運行起來后就會開啟一條線程,線程中會運行一個任務(wù)棧,當(dāng)Activity實例創(chuàng)建后就會放入任務(wù)棧中。Activity啟動模式的設(shè)置在AndroidManifest.xml文件中,通過配置Activity的屬性android:launchMode=""設(shè)置
    2015-12-12

最新評論