写一个unity的文字合成语音的API对应_msg为下面参考示例中参数这里是API的接口地址和参数: httpsgenshinvoicetopapispeaker=胡桃&msg=你好&format=wav&length=1&noise=05&noisew=09&sdp_ratio=02 下方是示例请求的样式请参照下面的代码样式改为前面genshinvoice的请求地址和参数请写一个完整的示例 us
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class TextToSpeech : MonoBehaviour { private string m_APIUrl = "https://genshinvoice.top/api"; private string m_Speaker = "胡桃"; private string m_Message = "你好"; private string m_Format = "wav"; private float m_Length = 1f; private float m_Noise = 0.5f; private float m_NoiseW = 0.9f; private float m_SdpRatio = 0.2f;
public void Start()
{
StartCoroutine(SendRequest());
}
private IEnumerator SendRequest()
{
string requestUrl = string.Format("{0}?speaker={1}&msg={2}&format={3}&length={4}&noise={5}&noisew={6}&sdp_ratio={7}",
m_APIUrl, m_Speaker, m_Message, m_Format, m_Length, m_Noise, m_NoiseW, m_SdpRatio);
UnityWebRequest www = UnityWebRequest.Get(requestUrl);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.LogError("Error sending request: " + www.error);
}
else
{
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(www);
if (audioClip != null)
{
// Play the audio clip
AudioSource audioSource = GetComponent<AudioSource>();
audioSource.clip = audioClip;
audioSource.Play();
}
else
{
Debug.LogError("Failed to get audio clip from response");
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/i9sd 著作权归作者所有。请勿转载和采集!