Unity文字转语音(使用RT-Voice PRO [2023.1.0])

本文介绍了如何在Unity项目中使用RtVioce插件实现文字转语音功能,包括导入插件、添加Prefab、代码调用以及控制语音播放、暂停和静默的操作。
摘要由CSDN通过智能技术生成

参考文章Unity插件——文字转朗读语音RtVioce插件功能/用法/下载_rtvoice-CSDN博客

一、使用步骤

1.导入进Unity(插件形式为 .unitypackage)

https://download.csdn.net/download/luckydog1120446388/88717512

2.添加所需Prefab

1).右键可以直接添加到 Hierarchy面板
在这里插入图片描述
2).添加后的样子
在这里插入图片描述

3.代码调用 RtVioceLocalTTS.cs

using Crosstales.RTVoice;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// creat by yxx 
/// 
/// https://blog.csdn.net/qq_43246529/article/details/109899257
/// </summary>
public class RtVioceLocalTTS : MonoBehaviour
{
    /*
 插件 - RtVioce
支持的Unity版本:5.3.1 及以上版本
功能:语音转文字
*不需要为自己的声音行事
*多个音色变换
*多个扬声器 同时对讲(说话)
*NPC聊天转换等。。。。   
?生成的音频可以存储到文件中。*/
    public InputField mTxtSpeakContent;
    public Button mBtnClick;
    private string mID;
    // Start is called before the first frame update
    void Start()
    {
        mBtnClick.onClick.AddListener(() =>
        {
            //Speak(mTxtSpeakContent.text);
            mID = Speaker.Instance.Speak(mTxtSpeakContent.text, null, Speaker.Instance.Voices[0]);
        });
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.P))
        {
            mID = Speaker.Instance.Speak("测试语音功能是否正常!Test whether the voice function is normal", null, Speaker.Instance.Voices[0]);
            //Speaker.Instance.SpeakNative("测试语音功能是否正常!Test whether the voice function is normal", Speaker.Instance.Voices[0], 1, 1, 1);
            Debug.Log("开始ID:" + mID);
            Speaker.Instance.OnSpeakStart += SpeakStart;
            Speaker.Instance.OnSpeakComplete += SpeakComplete;
            //Speaker.Instance.Speak("why");//测试语音功能是否正常!
        }
        if (Input.GetKeyDown(KeyCode.O))
        {
            Speaker.Instance.Silence(mID);
            mID = Speaker.Instance.Speak("我的目的是来测试id的", null, Speaker.Instance.Voices[0]);
            Debug.Log("开始ID:" + mID);
            Speaker.Instance.OnSpeakComplete += SpeakComplete;
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            Speaker.Instance.PauseOrUnPause();
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            Speaker.Instance.PauseOrUnPause();//
        }

        if (Input.GetKeyDown(KeyCode.Y))
        {
            Speaker.Instance.Silence(mID);//静默(介绍播放)
        }
    }
    private void SpeakStart(Crosstales.RTVoice.Model.Wrapper wrapper)
    {
        Debug.Log("开始播放ID:" + wrapper.Uid);
    }
    private void SpeakComplete(Crosstales.RTVoice.Model.Wrapper wrapper)
    {
        Debug.Log("完成ID:" + wrapper.Uid);
        if (wrapper.Uid.Equals(mID))
        {

        }
    }

    public void Speak(string _conetnt)
    {
        mID = Speaker.Instance.Speak(_conetnt, null, Speaker.Instance.Voices[0]);
    }
    /* public SpeechText SpeechText;
     // Start is called before the first frame update
     void Start()
     {
        *//* Speaker.Speak("哇塞,我可以说话了!", null, null, true, GUISpeech.Rate, GUISpeech.Volume, "", GUISpeech.Pitch);
         Speaker.OnSpeakComplete += speakEndMethod;
         Speaker.OnSpeakStart += speakStartMethod;*//*
         //取消调用
         //Speaker.OnSpeakStart-= speakStartMethod;
         //Speaker.OnSpeakComplete += speakEndMethod;

         //Speaker.Silence();//停止说话
     }
     private void speakStartMethod()//SpeakEventArgs e)
     {
         print("开始说话");
     }
     private void speakEndMethod()//SpeakEventArgs e)
     {
         print("完成说话");
     }
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             SpeechText.Speak();
         }
     }*/
}

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyDog阿祥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值