iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
Mobile Development

就是從無到有寫app系列 第 30

第30天~TTS(文字轉語音)+STT(語音轉文字)

  • 分享至 

  • xImage
  •  

TTS(文字轉語音)

開新檔案
https://ithelp.ithome.com.tw/upload/images/20220206/20119035ZUQJ3qc0uq.png

布置XML檔-

https://ithelp.ithome.com.tw/upload/images/20220206/201190355jwyJ4ygOB.png

按鈕也是要綁onClick-
https://ithelp.ithome.com.tw/upload/images/20220206/20119035z5DcZ0csvF.png

https://ithelp.ithome.com.tw/upload/images/20220206/20119035nOR2mSLwQs.png

步驟:
1.先宣告
2.初始化
3.加入偵聽器

https://ithelp.ithome.com.tw/upload/images/20220206/20119035pVz12Vnxmg.png

自己加入;

package com.huang.mytts;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    //先宣告
    TextToSpeech tts;
    EditText input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        input = findViewById(R.id.input);

        //加入偵聽器
        tts = new TextToSpeech(
                this,
                new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int i) {

                    }
                }
        );

    }

    public void onClick(View view) {
    }
}

https://ithelp.ithome.com.tw/upload/images/20220206/20119035HOAS6Rei7L.png

i改成 status


再來是~
1.再來如果是預設是支援的
2.因為我們都是說中文的所以是CHINESS
3.|按在ENTER+上面的|
4.音調語速是1就不高也不低

https://ithelp.ithome.com.tw/upload/images/20220206/20119035Z1YUWvJZ7j.png

@Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS){
            int language = tts.setLanguage(Locale.CHINESE);
            if(language == TextToSpeech.LANG_MISSING_DATA || language == TextToSpeech.LANG_NOT_SUPPORTED){
                Toast.makeText(MainActivity.this, "不支援", Toast.LENGTH_SHORT).show();
            }else{
                tts.setPitch(1);
                tts.setSpeechRate(1);
            }
        }
    }


然後是~
1-按下按鈕
2-馬上念=先來的先讀

/images/emoticon/emoticon03.gif

https://ithelp.ithome.com.tw/upload/images/20220206/20119035V7UtVtYO4a.png

public void onClick(View view) {
        String aa = input.getText().toString();
        tts.speak(aa, TextToSpeech.QUEUE_FLUSH, null);
    }


然後是~離開APP不要再念了...
/images/emoticon/emoticon03.gif

tts.stop();//停止目前的
tts.shutdown();//不要再念了

https://ithelp.ithome.com.tw/upload/images/20220206/20119035duXqJZUl9v.png


目前完整程式碼:

package com.huang.mytts;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener{

    TextToSpeech tts;
    EditText input;

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

        input = findViewById(R.id.input);
        tts = new TextToSpeech(this,this);
    }

    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS){
            int language = tts.setLanguage(Locale.CHINESE);
            if(language == TextToSpeech.LANG_MISSING_DATA || language == TextToSpeech.LANG_NOT_SUPPORTED){
                Toast.makeText(MainActivity.this, "不支援", Toast.LENGTH_SHORT).show();
            }else{
                tts.setPitch(1);
                tts.setSpeechRate(1);
            }
        }
    }

    public void onClick(View view) {
        String aa = input.getText().toString();
        tts.speak(aa, TextToSpeech.QUEUE_FLUSH, null);
    }

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

(https://ithelp.ithome.com.tw/upload/images/20220206/20119035nOR2mSLwQs.png)

步驟:
1.先宣告-
2.初始化
3.加入偵聽器

再來是~
1.再來如果是預設是支援的
2.因為我們都是說中文的所以是CHINESS
3.|按在ENTER+上面的|
4.音調語速是1就不高也不低

然後是~
1-按下按鈕
2-馬上念=先來的先讀

要用手機測試才可以聽到聲音
https://ithelp.ithome.com.tw/upload/images/20220206/20119035yLXmFspVTU.jpg


STT(語音轉文字)

開新檔案-
https://ithelp.ithome.com.tw/upload/images/20220206/20119035veUophO1aY.png

布置XML檔-

也是綁onClick

https://ithelp.ithome.com.tw/upload/images/20220206/20119035tUlkuPpOvt.png

https://ithelp.ithome.com.tw/upload/images/20220206/20119035VpaMw0Zk8i.png

語音輸入RecognizerIntent. 現在有許多app 都可以直接使用語音輸入, 直接講話就幫你轉成文字, 不用自己一個一個自己慢慢打~

但是中文英文夾雜不能~會亂掉

/images/emoticon/emoticon06.gif

ACTION_RECOGNIZE_SPEECH //分析語音
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//按照語言模式去分析.停頓會分段.分析
 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
//指定主要語言型態-依手機
startActivityForResult(intent, 200);
//啟動後等待手機回傳

返回結果如果是OK的解出來->而且是多字串解出來->把她顯示出來->有效的是第0段

if(resultCode == RESULT_OK && data != null){
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            show.setText(result.get(0));
        }

完整程式碼:

package com.huang.mystt;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextView show;

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

        show = findViewById(R.id.show);
    }

    public void onClick(View view) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

        startActivityForResult(intent, 200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK && data != null){
            ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            show.setText(result.get(0));
        }
    }
}

用模擬器跑看看-

https://ithelp.ithome.com.tw/upload/images/20220206/201190358UfeU4ylPb.png

按下按鈕-

https://ithelp.ithome.com.tw/upload/images/20220206/20119035uzZmkyI7DZ.png

允許他-

https://ithelp.ithome.com.tw/upload/images/20220206/20119035VHCWLlTzdW.png

按下按鈕說話:
https://ithelp.ithome.com.tw/upload/images/20220206/20119035Mimt03KUV3.png

也是要用手機測~

/images/emoticon/emoticon01.gif


今天聽到阿母要去上手機課~真的覺得自己也要活到老學到老~

這篇就是"花錢"...註冊安卓上架-2022/1/28的費用是台幣696

  1. https://play.google.com/console/signup

會先幫我們預設好是使用google的帳號
其實我自己常用的不是這個..QQ

希望...可以讓我繼續有鐵人發文的機會~拜託拜託

此篇的下一篇是:https://ithelp.ithome.com.tw/articles/10283884


上一篇
第29天-Picker:Date / Time選日期時間
下一篇
第31天~上架
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
不惑的胖虎
iT邦新手 4 級 ‧ 2021-10-24 21:17:45

只能給2了

Tzu iT邦新手 1 級 ‧ 2021-10-24 22:41:13 檢舉

很難說~ 我正在努力....希望...可以讓我繼續有鐵人發文的機會~拜託拜託QQ

我要留言

立即登入留言