【Unity】文字转语音

前言

Unity中文字转语音需要用到interop.speechlib.dllCustomMarshalers.dll插件
将这两个插件放到Plugins目录下
再将SpeechManager.cs导入到项目中
通过调用Speak(string strInfo)进行播放文字语音
通过调用StopSpeak()进行暂停语音播放

SpeechManager.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Windows.Speech;
using SpeechLib;

public static class SpeechContent
{
    public const string Wake = "醒醒醒醒,能听到我说话吗";
    public const string AskWhere = "你现在在哪里";
    public const string RaiseLeg = "抬一下左腿";
    public const string StartCure = "开始救治";
    public const string CreatInjuryTicket = "生成伤票";
}

public class SpeechManager : MonoBehaviour
{
    public static SpeechManager Instance { get; private set; }

    /// <summary>
    /// 语音识别成功后执行的回调
    /// </summary>
    public Action<string> SpeechActions;
    
    private KeywordRecognizer keywordRecognizer;
    
    private string[] Keywords_array;

    private SpVoice spv;

    private void Awake()
    {
        Instance = this;
        InitSpeech();
        InitSpVoice();
    }

    /// <summary>
    /// 初始化语音转文字
    /// </summary>
    private void InitSpVoice()
    {
        spv = new SpVoice();
        //找到kangkang作为与语音
        for (int i = 0; i < spv.GetVoices().Count; i++)
        {
            Debug.Log(spv.GetVoices().Item(i).GetDescription());
            if (spv.GetVoices().Item(i).GetDescription().Contains("Huihui"))
            {
                spv.Voice = spv.GetVoices().Item(i);
                Debug.Log("设置语音 " + spv.GetVoices().Item(i).GetDescription());
            }
        }

        spv.Volume = 100;
    }

    /// <summary>
    /// 初始化语音识别
    /// </summary>
    private void InitSpeech()
    {
        Keywords_array = new string[3];
        Keywords_array [0] = SpeechContent.Wake;
        Keywords_array [1] = SpeechContent.AskWhere;
        Keywords_array [2] = SpeechContent.RaiseLeg;
        
        keywordRecognizer = new KeywordRecognizer(Keywords_array);
        keywordRecognizer.OnPhraseRecognized += OnKeywordsRecognized;
        keywordRecognizer.Start ();
    }

    private void OnKeywordsRecognized(PhraseRecognizedEventArgs args)
    {
        Debug.Log ("Keyword: " + args.text + "; Confidence: " + args.confidence + "; Start Time: " + args.phraseStartTime + "; Duration: "  + args.phraseDuration);
        SpeechActions?.Invoke(args.text);
    }
    
    public void Speak(string content)
    {
        spv.Speak(string.Empty,SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        spv.Speak(content, SpeechVoiceSpeakFlags.SVSFlagsAsync);
    }

    /// <summary>
    /// 暂停播报
    /// </summary>
    public void StopSpeak()
    {
        spv.Speak(string.Empty,SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
    }

    private void OnDestroy()
    {
        spv.Volume = 0;
       
    }
    
}

使用方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public Button btn_Speak;
    public Button btn_StopPlay;
    void Start()
    {
        btn_Speak.onClick.AddListener(delegate
        {
            SpeechManager.Instance.Speak("你好");
        });
        
        btn_StopPlay.onClick.AddListener(delegate
        {
            SpeechManager.Instance.StopSpeak();
        });
    }
    
    
}



链接:https://pan.baidu.com/s/1nOBg2fQH3IKURS6x_GTEIA?pwd=cc89
提取码:cc89
–来自百度网盘超级会员V7的分享

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Unity文字转语音是指在Unity开发环境下使用插件或代码实现将文字转换为语音的功能。在Unity中,有一些插件可以实现这一功能,比如RtVioce插件。通过使用这些插件,开发者可以在Unity中将文字转换为语音,并可以根据需要选择不同的音色和扬声器进行转换和播放。具体的实现方式可以通过调用TTS功能来实现,比如使用TextToSpeech.Instance.StartSpeaking的方法来将指定的文字转换为语音。转换完成后,生成的音频可以存储到文件中,并可以通过使用audio source组件进行播放。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [unity文字转语音插件.rar](https://download.csdn.net/download/qq_43505432/16751694)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Unity3D HoloLens2 中文文字转语音即语音合成(语音提示)功能](https://blog.csdn.net/qq_33789001/article/details/112345062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

趙銭孫李

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

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

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

打赏作者

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

抵扣说明:

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

余额充值