你可以使用 Unity 的 UnityWebRequest 来获取页面中的 WAV 文件,并使用 AudioClip 来播放音频。以下是修改后的完整代码示例:

using System;
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;

public class GenshinTextToSpeech : TTS
{
    #region 参数定义
    [Header("朗读声音设置")]
    public string speaker = "胡桃";
    [Header("音频格式设置")]
    public string format = "wav";
    [Header("音频长度设置")]
    public float length = 1f;
    [Header("噪声设置")]
    public float noise = 0.5f;
    [Header("噪声权重设置")]
    public float noisew = 0.9f;
    [Header("声调比例设置")]
    public float sdp_ratio = 0.2f;
    #endregion

    private void Awake()
    {
        m_PostURL = "https://genshinvoice.top/api";
    }

    public override void Speak(string _msg, Action<AudioClip> _callback)
    {
        StartCoroutine(GetVoice(_msg, _callback));
    }

    public override void Speak(string _msg, Action<AudioClip, string> _callback)
    {
        StartCoroutine(GetVoice(_msg, _callback));
    }

    private IEnumerator GetVoice(string _msg, Action<AudioClip> _callback)
    {
        stopwatch.Restart();
        string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);

        using (UnityWebRequest speechRequest = UnityWebRequestMultimedia.GetAudioClip(textToSpeechRequestBody, AudioType.WAV))
        {
            yield return speechRequest.SendWebRequest();

            if (speechRequest.result == UnityWebRequest.Result.Success)
            {
                AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
                _callback(audioClip);
            }
            else
            {
                Debug.LogError("语音合成失败: " + speechRequest.error);
            }
        }

        stopwatch.Stop();
        Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
    }

    private IEnumerator GetVoice(string _msg, Action<AudioClip, string> _callback)
    {
        stopwatch.Restart();
        string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);

        using (UnityWebRequest speechRequest = UnityWebRequestMultimedia.GetAudioClip(textToSpeechRequestBody, AudioType.WAV))
        {
            yield return speechRequest.SendWebRequest();

            if (speechRequest.result == UnityWebRequest.Result.Success)
            {
                AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
                _callback(audioClip, _msg);
            }
            else
            {
                Debug.LogError("语音合成失败: " + speechRequest.error);
            }
        }

        stopwatch.Stop();
        Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
    }

    public string GenerateTextToSpeech(string speaker, string msg, string format, float length, float noise, float noisew, float sdp_ratio)
    {
        string urlParams = string.Format("?speaker={0}&text={1}&format={2}&length={3}&noise={4}&noisew={5}&sdp_ratio={6}", speaker, msg, format, length, noise, noisew, sdp_ratio);
        string url = m_PostURL + urlParams;

        return url;
    }
}

使用方法:

GenshinTextToSpeech tts = GetComponent<GenshinTextToSpeech>();
tts.Speak("你好", audioClip =>
{
    // 播放音频
    AudioSource audioSource = GetComponent<AudioSource>();
    audioSource.clip = audioClip;
    audioSource.Play();
});

请确保你的 Unity 项目中已经添加了 AudioSource 组件来播放音频。

Unity 使用 UnityWebRequest 获取并播放 WAV 音频文件

原文地址: http://www.cveoy.top/t/topic/jCf1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录