TextToSpeech

// 意义,api接口,使用流程,完整代码。参考目录

意义:TextToSpeech,从文本到语音的缩写。通过语音SDK或安装语音APK来达到让机器说话

方法:

  1.构造方法:

      TextToSpeech(Context context, TextToSpeech.OnInitListener listener)

        Context:上下文

        TextToSpeech.OnInitListener:加载监听接口

  2.主要方法

      //播放语音

      textToSpeech.speak(String content, int queueMode, Bundle param, String utteranceId)

         content:文本内容

         queueMode:队列方式(TextToSpeech.QUEUE_ADD 播放完成后才播放,TextToSpeech.QUEUE_FLUSH 丢弃之前,立即播放)

         param:设置TTS参数,可以是null(KEY_PARAM_STREAM:音频通道,可以是:STREAM_MUSIC、STREAM_NOTIFICATION、STREAM_RING等 ,KEY_PARAM_VOLUME:音量大小,0-1f)  

        utteranceId:当前朗读文本的id

 

      //停止播放

      textToSpeech.stop()

 

      //关闭,释放资源

      textToSpeech.shutdown()

 

      //设置音调,值越大越像女生,值越小越像男生。谷歌语音apk范围:0-2.0f  科大讯飞无

      textToSpeech.setPitch(float pitch)

 

      //设置语速,谷歌语音apk范围:0-5.0f ,科大讯飞:1.0-1.5-2.0f

      textToSpeech.setSpeechRate(float rate)

      

      //设置语音包

      int  textToSpeech.setLanguage(Locale.CHINA)

        返回值与TextToSpeech.LANG_NOT_SUPPORTED 和 TextToSpeech.LANG_MISSING_DATA作对比

  3.步骤:

    1.实现TextToSpeech.OnInitListener接口,重写init()方法。对textText加载状态监控

    @Override
    public void onInit(int status) {
        if( status == TextToSpeech.SUCCESS ){
            int setLanguageResult = textToSpeech.setLanguage(Locale.CHINA);
            if( setLanguageResult == TextToSpeech.LANG_NOT_SUPPORTED || setLanguageResult == TextToSpeech.LANG_MISSING_DATA ){
                Toast.makeText(this,"不支持中文语音包",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this,"加载tts失败",Toast.LENGTH_SHORT).show();
        }
    }

  

    2.初始化TextToSpeech并设置语速和音高

textToSpeech = new TextToSpeech(this, this);
textToSpeech.setPitch(Float.parseFloat(pitch)); //音高
textToSpeech.setSpeechRate(Float.parseFloat(rate)); //语速

  

    3.调用textToSpeech.speak(String content, TextToSpeech.QUEUE_ADD, null, "0")

 

    4.activity推出后释放资源

  @Override
    protected void onStop() {
        super.onStop();
        textToSpeech.stop();
        textToSpeech.shutdown();
    }

 

完整代码

public class MyTTSActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {

    private TextToSpeech textToSpeech;
    private Button btn1,btnRate , btnPitch;
    private EditText edtContent, edtRate, edtPitch;
    private String rate = "1.0", pitch = "1.0";
    int tId = 0;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_t_t_s);
        textToSpeech = new TextToSpeech(this, this);

        //google文本转语音:速度:0-5.0f ,音高0-2.0f
        //科大讯飞文本转语音:速度:1-1.5-2f,音高无
        /**
         * content:内容
         * queueMode:队列方式
         *      QUEUE_ADD:播放完成之后才开始播报本次内容
         *      QUEUE_FLUSH:丢掉之前的播报内容,立即播报本次内容
         * params:设置TTS参数,可以是null
         *      KEY_PARAMS_STEAM:音频通道,可以是:STEAM_MUSIC,STEAM_NOTIFICATION,STEAM_RING等
         *      KEY_PARAMS_VOLUME:音量大小,0-1f  1-1.5-2f
         *
         *
         * utteranceId:设置朗读文本的id
         *
         * */

//        textToSpeech.speak("哈哈哈",TextToSpeech.QUEUE_FLUSH,null,"0");
//
//        //停止tts朗读
//        textToSpeech.stop();
//
//        //关闭,释放资源
//        textToSpeech.shutdown();
//
//        //设置音调,值越大,声音越尖锐(女声)。值越小变成男声,1.0是常规
//        textToSpeech.setPitch(0.5f);
//
//        //设定语速,默认1.0是正常语速
//        textToSpeech.setSpeechRate(1.5f);

        btn1 = findViewById(R.id.btn1);
        btnPitch = findViewById(R.id.btnPitch);
        btnRate = findViewById(R.id.btnRate);

        edtContent = findViewById(R.id.edtContent);
        edtPitch = findViewById(R.id.edtPitch);
        edtRate = findViewById(R.id.edtRate);
        edtPitch.setText(pitch);
        edtRate.setText(rate);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = edtContent.getText().toString().trim();
                textToSpeech.speak(content,TextToSpeech.QUEUE_ADD,null,tId++ +"");
            }
        });

        btnPitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pitch = edtPitch.getText().toString().trim();
                textToSpeech.setPitch(Float.parseFloat(pitch));
                edtPitch.setText(pitch);
            }
        });

        btnRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rate = edtRate.getText().toString().trim();
                textToSpeech.setSpeechRate(Float.parseFloat(rate));
                edtRate.setText(rate); //1.0,1.5,2.0
            }
        });
    }

    @Override
    public void onInit(int status) {
        if( status == TextToSpeech.SUCCESS ){
            int setLanguageResult = textToSpeech.setLanguage(Locale.CHINA);
            if( setLanguageResult == TextToSpeech.LANG_NOT_SUPPORTED || setLanguageResult == TextToSpeech.LANG_MISSING_DATA ){
                Toast.makeText(this,"不支持中文语音包",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this,"加载tts失败",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        textToSpeech.stop();
        textToSpeech.shutdown();
    }
}

 

参考博客:https://www.jianshu.com/p/2c75343c9dc9

posted @ 2023-12-14 12:01  remix_alone  阅读(99)  评论(0编辑  收藏  举报