Android學習之TTS踩坑筆記


•前言

  最近在做一款英文詞典的 APP,做到語音模塊的時候,我裂開,從網上搜索了各種博客,各種瞎搗鼓,模擬器就是不發音;

  輾轉反側數日,終於讓我找到解決之法,接下來就本次測試列出一些需要(必須)解決的問題:

  1. 模擬器如何聯網
  2. 模擬器如何安裝 TTS 引擎?
  3. 模擬器如何安裝已下載好的 apk 文件?

  接下來就這些問題做出一一解答;

•准備工作

  我在 Genymotion 上下載了兩個模擬器,分別為 Android 7.0 , Android 8.0 , Android 9.0;

  准備好如下文件,我放在了網盤中,需要的話,自行下載提取【網盤鏈接提取碼:t2li】;

•模擬器如何聯網?

  打開【Oracle VM VirtualBox】,選中 Android 7.0 設備,點擊【設置】;

  來到如下界面,選中【網絡】->【網卡 1】,點擊【高級】,將【混雜模式】更改為全部允許;

  接着打開【網卡 2】,將連接方式更改為【NAT】;

  重啟模擬器,打開瀏覽器輸入 www.baidu.com 來檢查網絡連接情況;

  雖然 WIFI 上有個的感嘆號,但絲毫不影響模擬器聯網;

  Android 8.0 , Android 9.0 聯網同上;

  問題一解決 √;

•模擬器如何安裝 apk?

  將下載好的【qq.apk】以拖拽的方式安裝到 Android 7.0 上;

  你會發現,他會給你報錯;

  意思是說 Genymotion 是基於 X86 的,不支持 ARM 架構,所以基於 ARM 架構 的應用就無法安裝。

解決方案

  將【ARM_Translation_low_version.zip】拖拽到 Android 7.0 模擬器中;

  如果出現如下界面:

  恭喜你,安裝成功,接下需要你重啟該模擬器;

  如果沒有出現該界面,那么,就需要檢查一下文件路徑以及文件名:

  • 文件路徑不能包含中文,參考一下我的路徑 E:\TTS\ARM_Translation_low_version.zip
  • 文件名不能包含空格,比如把【ARM_Translation_low_version.zip】改成了【ARM_Translation low_version.zip】,這些操作都會導致拖放失敗

  此時,再次將【qq.apk】拖拽到 Android 7.0 上,拖放前一定要記得重啟模擬器;

  Android 8.0 , Android 9.0 模擬器安裝 apk 方式同上,只需要將【ARM_Translation】更改為對應的【ARM】即可;

  問題解決√;

•模擬器如何安裝 TTS 引擎?

  解決了第二個問題后,這個豈不變得 so easy~

  將【GoogleTTS.apk】拖放到模擬器上,然后打開【設置】->【無障礙】->【文字轉語音(TTS)輸出】,

  並將【首選引擎】更改為 Google 文字轉語音引擎;

  接下來就是檢驗階段,新建一個 TestTTS 項目,添加如下代碼;

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/main_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input something"/>

    <Button
        android:id="@+id/main_btn_speech"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發音" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private TextToSpeech textToSpeech;
    private EditText editText;
    private Button mBtnSpeech;

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

        editText = findViewById(R.id.main_text);
        mBtnSpeech = findViewById(R.id.main_btn_speech);

        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {

                    //設置發音的語言
                    int result = textToSpeech.setLanguage(Locale.ENGLISH);

                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Toast.makeText(MainActivity.this, "發音失敗", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "發音成功", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });

        mBtnSpeech.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                textToSpeech.speak(editText.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
            }
        });
    }
}

  編譯運行,自己本地測試一下,我這是可行的。

  由於我找的這個【GoogleTTS.apk】支持的 Android 版本需要 ≥ 8.0,所以,要測試的話,還是要在 Android 8.0 或 Android 9.0 上測試。

•寫在最后

  兜兜轉轉搗鼓了好幾天,還好成功了,感謝大佬們寫的博客,助我一臂之力;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM