文字转语音 两种方法:TextToSpeech、科大讯飞

TextToSpeech核心代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mBinding.setOnClick(this);
        mTextToSpeech = new TextToSpeech(this,this);
    }

 

    private void speakOut() {
        if (mTextToSpeech!= null && !mTextToSpeech.isSpeaking()){
//            Bundle bundle = new Bundle();
//            bundle.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME,1.0f);
            //朗读,注意这里三个参数的added in API level 4   四个参数的added in API level 21
//            mTextToSpeech.speak(mBinding.editText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
            mTextToSpeech.speak(mBinding.editText.getText().toString(),TextToSpeech.QUEUE_FLUSH,null,"");
        }
    }

    /**
     * 初始化语音播放
     * @param status
     */
    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS){
            // 设置音调,1.0是常规
            mTextToSpeech.setPitch(1.0f);
            //设定语速 ,默认1.0正常语速
            mTextToSpeech.setSpeechRate(1.0f);
            int result = mTextToSpeech.setLanguage(Locale.CHINA);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED){
                Toast.makeText(this,"数据丢失或不支持",Toast.LENGTH_SHORT).show();
            }
        }
    }

科大讯飞:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_xun_fei);
        mBinding.setOnClick(this);
        //初始化科大讯飞
        SpeechUtility.createUtility(getApplicationContext(), SpeechConstant.APPID + "=5b558975");
        mSynthesizer = SpeechSynthesizer.createSynthesizer(this, this);
    }

    private void speak() {
        mSynthesizer.startSpeaking(mBinding.editTextXf.getText().toString(),mSynthesizerListener);
    }

    /*
        合成回调
     */
   private SynthesizerListener mSynthesizerListener = new SynthesizerListener() {
       @Override
       public void 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 (speechError == null){//播放完成

            }else {

            }
       }

       @Override
       public void onEvent(int i, int i1, int i2, Bundle bundle) {
           // 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因
           // 若使用本地能力,会话id为null
       }
   };


    @Override
    public void onInit(int i) {
        if (i != ErrorCode.SUCCESS){
            return;//初始化失败
        }
        mSynthesizer.setParameter(SpeechConstant.ENGINE_MODE, SpeechConstant.MODE_MSC);
        mSynthesizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
        mSynthesizer.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
        mSynthesizer.setParameter(SpeechConstant.VOICE_NAME, voicerLocal);
    }

 

代码较为简单,有不足之处大家可以留言交流

完整代码见GitHub

 

发布了20 篇原创文章 · 获赞 9 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览