Unity3D 使用Windows系统朗读文字

1、这里主要使用到两个动态库:Interop.SpeechLib.dll、CustomMarshalers.dll,大家可以自行下载,或者在https://download.csdn.net/download/WenHuiJun_/87287758下载。

2、把dll放进项目中,编写代码即可。

不需要挂载在场景中:

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


public class VoiceManager
{
    public SpVoice voice = new SpVoice();//语音朗读声音
    bool isVoicePause = false;
    static VoiceManager _instance;
    public static VoiceManager Instance()
    {
        if(_instance==null)
            _instance=new VoiceManager();
        return _instance;
    }
    /// <summary>
    /// 获取语音库
    /// </summary>
    /// <returns>List<string></returns>
    public List<string> getDescription()
    {
        List<string> list = new List<string>();
        ISpeechObjectTokens obj = voice.GetVoices();
        int count = obj.Count;//获取语音库总数
        for (int i = 0; i < count; i++)
        {
            string desc = obj.Item(i).GetDescription(); //遍历语音库
            list.Add(desc);
        }
        return list;
    }
    /// <summary>
    /// 设置当前使用语音库
    /// </summary>
    /// <returns>bool</returns>
    public int setDescription(string name = null)
    {
        ISpeechObjectTokens obj = voice.GetVoices();
        //获取语音库总数
        int count = obj.Count;
        int index = 0;
        if (name == null)
            return 0;
        for (int i = 0; i < count; i++)
        {
            //遍历语音库
            string desc = obj.Item(i).GetDescription();
            if (desc.Contains(name))
            {
                voice.Voice = obj.Item(i);
                index = i;
            }
        }
        return index;
    }
    /// <summary>
    /// 语音朗读按钮事件
    /// </summary>
    public void SpeakText(string str, string voiceName = null, int voiceRate = 1)
    {
        if (!string.IsNullOrWhiteSpace(str))
        {
            if (getDescription().Count != 0)
            {
                voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(setDescription(voiceName));//设置中文语音
                voice.Rate = voiceRate;//语音速度,范围-10到10,默认是0
                voice.Volume = 100;//语音音量,范围0到100,默认是100
                //voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);//不开子线程,朗读不卡(朗读内容,如果正在朗读,则停止,然后朗读当前的内容,用的异步)
                voice.Speak(str, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
            }
        }
        isVoicePause = false;
    }
    /// <summary>
    /// 暂停语音
    /// </summary>
    public void ToPauseVoice()
    {
        if (!isVoicePause)
        {
            voice.Pause();
            isVoicePause = true;
        }
    }
    /// <summary>
    /// 继续播放语音
    /// </summary>
    public void ToResumeVoice()
    {
        if (isVoicePause)
        {
            voice.Resume();
            isVoicePause = false;
        }
    }
    /// <summary>
    /// 关闭语音朗读
    /// </summary>
    public void StopSpeakText()
    {
        voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
    }
}

需要挂载在场景中:

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

public class VoiceBtn : MonoBehaviour
{
    private Button voiceBtn;
    VoiceManager voiceManager;

    private void Start()
    {
        voiceManager = VoiceManager.Instance();
        voiceBtn = GetComponent<Button>();
        voiceBtn.onClick.AddListener(delegate
        {
           voiceManager. SpeakText("Hello");
        });
    }
    private void Update()
    {
        //if ((int)manager.voice.Status.RunningState == 1) //0:朗读暂停; 1:朗读完成; 2:正在朗读)
        //{
        //    
        //}
    }
    private void OnDisable()
    {
        voiceManager.StopSpeakText();
    }
    private void OnApplicationQuit()
    {
        voiceManager.StopSpeakText();
    }
}

3、需要增加角色,可以参考https://download.csdn.net/download/WenHuiJun_/87287758

猜你喜欢

转载自blog.csdn.net/WenHuiJun_/article/details/128301598