android 語音合成(文字轉(zhuǎn)語音播放)

android自帶

首先android自帶文字轉(zhuǎn)語音支持: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) {
               //設(shè)置播放語言
                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("需要轉(zhuǎn)化的文字",TextToSpeech.QUEUE_ADD,null);
                }
               
            } else {
                Log.e("TAG", "初始化失敗");
            }

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

科大訊飛語音合成

https://www.xfyun.cn/
和其他第三方技術(shù)支持相同
1.注冊,登錄,申請應(yīng)用
2.閱讀文檔,下載sdk,下載demo
3.語音合成步驟:

  • 初始化,設(shè)置參數(shù)(發(fā)音人,大小,語速,)
  • 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 {

    // 默認發(fā)音人
    private String voicer = "xiaoyan";
    private String TAG = "VoiceActivity";

    // 引擎類型
    private String mEngineType = SpeechConstant.TYPE_CLOUD;
    private SpeechSynthesizer speechSynthesizer;
    private List<String> 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 {
                    // 初始化成功,之后可以調(diào)用startSpeaking方法
                    // 注:有的開發(fā)者在onCreate方法中創(chuàng)建完合成對象之后馬上就調(diào)用startSpeaking進行合成,
                    // 正確的做法是將onCreate中的startSpeaking調(diào)用移至這里
                    setParams();
                }

            }
        });
    }

    private void initView() {
        Button ttPlayButton = findViewById(R.id.ttsplay);
        ttPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                say("翻涌眼底的光影");
                say("和熟悉的聲音");
                say("經(jīng)過多少練習(xí),才能成為這樣的你");
            }
        });
    }

    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<String> 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");
        // 設(shè)置在線合成發(fā)音人
        speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, voicer);
        //設(shè)置合成語速
        speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
        //設(shè)置合成音調(diào)
        speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");
        //設(shè)置合成音量
        speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容