第30天~TTS(文字转语音)+STT(语音转文字)

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


<<:  D30 - Python入门挑战end?!

>>:  【Day 30】接下来要继续做的事 + 还没完成的 WaitGroup 版 Merge Sort

# Day 30 Commencement: I open at the close

哇!不知不觉就到第 30 天了,来回顾一下这 30 天的旅程吧! 简单回顾 自己订的铁人赛主题是阅读...

Day 30 完赛心得

在开始铁人赛之後才发现这个月不该比铁人赛的 这个月的事情比平常都还要多 虽然硬着头皮写完了30天的文...

Array筛选特定值

缘由: 对本科生或天资聪颖的人来说这问题真的没什麽,但对於初学者且还只有教科书程度的我,要我从一组复...

Day12-slot插槽

元件的概念大概有了部分的了解,同一个元件可以重复利用,但这句话好像有个问题,不同页面使用相同架构的元...

【第二十天 - Graph 介绍】

Q1. Graph 是什麽 Graph 定义:一个 graph 由 数个点( vertex )与数个...