[Unreal Engine] UE4/UE5 iFLYTEK Text Synthesis Speech

1. Link address

Link: https://pan.baidu.com/s/15Qoc48x3DLpw4eW1qHXInQ 
Extraction code: jqpx 

B station video link: https://space.bilibili.com/449549424?spm_id_from=333.1007.0.0 

2. Case introduction

Step 1: First enter the Xunfei open platform to register an account, then create an application, name it according to your own ideas, and an APPID will be generated. For details, refer to how UE4 accesses the voice recognition of iFLYTEK_ue4 科大信息Flying Speech Recognition_Flying Pig Blog-CSDN Blog

Step 2: Download iFLYTEK’s Speech-to-Text SDK, open AndriodStudio and run

 Refer to the following code written

private static String TAG = "TtsDemo";  
 // 语音合成对象
 private SpeechSynthesizer mTts;

 // 默认发音人
 private String voicer="xiaoyan";
 
 private String[] cloudVoicersEntries;
 private String[] cloudVoicersValue ;
 
 //缓冲进度
 private int mPercentForBuffering = 0; 
 //播放进度
 private int mPercentForPlaying = 0;
 
 // 云端/本地选择按钮
 private RadioGroup mRadioGroup;
 // 引擎类型
 private String mEngineType = SpeechConstant.TYPE_CLOUD;
 // 语音+安装助手类
 //ApkInstaller mInstaller ;
 
 private Toast mToast;
 private SharedPreferences mSharedPreferences;
 
 @SuppressLint("ShowToast")
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.ttsdemo);
  initLayout();
  // 初始化合成对象
  mTts = SpeechSynthesizer.createSynthesizer(this, mTtsInitListener);
  mSharedPreferences = getSharedPreferences("com.iflytek.setting", Activity.MODE_PRIVATE);
  mToast = Toast.makeText(this,"",Toast.LENGTH_SHORT);
 }

 /**
  * 初始化Layout。
  */
 private void initLayout() {
  findViewById(R.id.tts_play).setOnClickListener(this);
  
  findViewById(R.id.tts_cancel).setOnClickListener(this);
  findViewById(R.id.tts_pause).setOnClickListener(this);
  findViewById(R.id.tts_resume).setOnClickListener(this);
 } 

 @Override
 public void onClick(View view) {
  switch(view.getId()) {

  // 开始合成
  case R.id.tts_play:
   String text = ((EditText) findViewById(R.id.tts_text)).getText().toString();
   // 设置参数
   setParam();
   int code = mTts.startSpeaking(text, mTtsListener);
   if (code != ErrorCode.SUCCESS) {
    if(code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED){
     //未安装则跳转到提示安装页面
     //mInstaller.install();
    }else {
     showTip("语音合成失败,错误码: " + code); 
    }
   }
   break;
  // 取消合成
  case R.id.tts_cancel:
   mTts.stopSpeaking();
   break;
  // 暂停播放
  case R.id.tts_pause:
   mTts.pauseSpeaking();
   break;
  // 继续播放
  case R.id.tts_resume:
   mTts.resumeSpeaking();
   break;

  }
 }

 /**
  * 初期化监听。
  */
 private InitListener mTtsInitListener = new InitListener() {
  @Override
  public void onInit(int code) {
   Log.d(TAG, "InitListener init() code = " + code);
   if (code != ErrorCode.SUCCESS) {
          showTip("初始化失败,错误码:"+code);
         }  
  }
 };

 /**
  * 合成回调监听。
  */
 private SynthesizerListener mTtsListener = new SynthesizerListener() {
  @Override
  public void onSpeakBegin() {
   showTip("开始播放");
  }

  @Override
  public void onSpeakPaused() {
   showTip("暂停播放");
  }

  @Override
  public void onSpeakResumed() {
   showTip("继续播放");
  }

  @Override
  public void onBufferProgress(int percent, int beginPos, int endPos,
    String info) {
   mPercentForBuffering = percent;
   mToast.setText(String.format(getString(R.string.tts_toast_format),
     mPercentForBuffering, mPercentForPlaying));
   
   mToast.show();
  }

  @Override
  public void onSpeakProgress(int percent, int beginPos, int endPos) {
   mPercentForPlaying = percent;
   showTip(String.format(getString(R.string.tts_toast_format),
     mPercentForBuffering, mPercentForPlaying));
  }

  @Override
  public void onCompleted(SpeechError error) {
   if(error == null)
   {
    showTip("播放完成");
   }
   else if(error != null)
   {
    showTip(error.getPlainDescription(true));
   }
  }

  @Override
  public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
   
  }
 };

 private void showTip(final String str){
  runOnUiThread(new Runnable() {
   @Override
   public void run() {
    mToast.setText(str);
    mToast.show();
   }
  });
 }

 /**
  * 参数设置
  * @param param
  * @return
  */
 private void setParam(){
  
  //设置合成
  if(mEngineType.equals(SpeechConstant.TYPE_CLOUD))
  {
   mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
   //设置发音人
   mTts.setParameter(SpeechConstant.VOICE_NAME,voicer);
  }else {
   mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
   //设置发音人 voicer为空默认通过语音+界面指定发音人。
   mTts.setParameter(SpeechConstant.VOICE_NAME,"");
  }
  
  //设置语速
  mTts.setParameter(SpeechConstant.SPEED,mSharedPreferences.getString("speed_preference", "30"));

  //设置音调
  mTts.setParameter(SpeechConstant.PITCH,mSharedPreferences.getString("pitch_preference", "50"));

  //设置音量
  mTts.setParameter(SpeechConstant.VOLUME,mSharedPreferences.getString("volume_preference", "80"));
  
  //设置播放器音频流类型
  mTts.setParameter(SpeechConstant.STREAM_TYPE,mSharedPreferences.getString("stream_preference", "3"));
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  mTts.stopSpeaking();
  // 退出时释放连接
  mTts.destroy();
 }

Step 3: Replace the lib and srcy files in the downloaded SDK

 Step 4: Create a plug-in in UE4, and associate UE4 code with Java code through APL_xml

 Step 5: Write C++ and blueprints, and call C++ functions in the blueprints

 Step 6: Create UMG,

 result:

Guess you like

Origin blog.csdn.net/qq_43021038/article/details/130362324