android 语音合成(文字转语音播放)

android自带

首先android自带文字转语音支持:TextToSpeech
但是在6.0之前不支持中文播放

 private TextToSpeech tts;
 private void play() {
        tts = new TextToSpeech(this,new listener());
    }
 private class listener implements TextToSpeech.OnInitListener {

        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
               //设置播放语言
                int result = tts.setLanguage(Locale.CHINESE);
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
                    Toast.makeText(VoiceActivity.this, "不支持", Toast.LENGTH_SHORT).show();
                }else if(result == TextToSpeech.LANG_AVAILABLE){
                  //初始化成功之后才可以播放文字
                  //否则会提示“speak failed: not bound to tts engine
                  //TextToSpeech.QUEUE_ADD会将加入队列的待播报文字按顺序播放
                  //TextToSpeech.QUEUE_FLUSH会替换原有文字
                  tts.speak("需要转化的文字",TextToSpeech.QUEUE_ADD,null);
                }
               
            } else {
                Log.e("TAG", "初始化失败");
            }

        }
    public void stopTTS() {
        if ( tts  != null) {
             tts .shutdown();
             tts .stop();
             tts = null;
        }
    }

科大讯飞语音合成

https://www.xfyun.cn/
和其他第三方技术支持相同
1.注册,登录,申请应用
2.阅读文档,下载sdk,下载demo
3.语音合成步骤:

  • 初始化,设置参数(发音人,大小,语速,)
  • startSpeaking()
  • 注意,这个并不支持添加多条文字,顺序播放,需要自己处理
  • 遇到的问题:
    SpeechSynthesizer.createSynthesizer(this, this);等于null问题
    问题原因:因为android jar包默认路径是jniLibs,而我把包放入了libs里
    解决办法:build.gradle中加入下面代码:
sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

代码:

package com.tencent.wstt.demo1;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

public class VoiceActivity extends AppCompatActivity {

    // 默认发音人
    private String voicer = "xiaoyan";
    private String TAG = "VoiceActivity";

    // 引擎类型
    private String mEngineType = SpeechConstant.TYPE_CLOUD;
    private SpeechSynthesizer speechSynthesizer;
    private List playList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice);
        initView();
        init();
    }

    private void init() {
        playList = new ArrayList();
        speechSynthesizer = SpeechSynthesizer.createSynthesizer(this, new InitListener() {

            @Override
            public void onInit(int i) {
                Log.d(TAG, "InitListener init() code = " + i);
                if (i != ErrorCode.SUCCESS) {
                    Toast.makeText(VoiceActivity.this,"初始化失败:"+i,Toast.LENGTH_LONG).show();
                } else {
                    // 初始化成功,之后可以调用startSpeaking方法
                    // 注:有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成,
                    // 正确的做法是将onCreate中的startSpeaking调用移至这里
                    setParams();
                }

            }
        });
    }

    private void initView() {
        Button ttPlayButton = findViewById(R.id.ttsplay);
        ttPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                say("翻涌眼底的光影");
                say("和熟悉的声音");
                say("经过多少练习,才能成为这样的你");
            }
        });
    }

    private void say(String text) {
        playList.add(text);
        if(speechSynthesizer.isSpeaking()){
            Log.e(TAG,"isSpeaking");
            return;
        }
        startSpeaking(text);
    }

    private void startSpeaking(String text) {
        speechSynthesizer.startSpeaking(text, new SynthesizerListener() {
            @Override
            public void onSpeakBegin() {
                Log.e(TAG,"onSpeakBegin");
            }

            @Override
            public void onBufferProgress(int i, int i1, int i2, String s) {

            }

            @Override
            public void onSpeakPaused() {

            }

            @Override
            public void onSpeakResumed() {

            }

            @Override
            public void onSpeakProgress(int i, int i1, int i2) {

            }

            @Override
            public void onCompleted(SpeechError speechError) {

                if(playList != null && playList.size()>0){
                    Iterator iterator = playList.iterator();
                    if (iterator.hasNext()) {
                        String str = iterator.next();
                        iterator.remove();
                    }

                    if(playList.size()>0){
                        startSpeaking(playList.get(0));
                    }
                }
            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        });

    }

    private void setParams() {
        speechSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        speechSynthesizer.setParameter(SpeechConstant.TTS_DATA_NOTIFY, "1");
        // 设置在线合成发音人
        speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, voicer);
        //设置合成语速
        speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
        //设置合成音调
        speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");
        //设置合成音量
        speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");
    }

}

你可能感兴趣的:(android 语音合成(文字转语音播放))