使用JSONP类型发送请求需要修改以下几个地方:

  1. 将UnityWebRequest.Get()方法改为UnityWebRequest.GetRequestUri()方法,用于获取请求的完整URL地址。
  2. 将DownloadHandlerAudioClip类改为DownloadHandlerBuffer类,用于接收返回的数据。
  3. 在URL的末尾添加回调函数的参数名(例如:callback=),用于接收返回的数据。

修改后的代码如下所示:

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

public class GenshinTextToSpeech : TTS
{
    #region 参数定义
    // 省略参数定义部分

    #endregion

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

    // 省略Speak方法和GenerateTextToSpeech方法

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

        using (UnityWebRequest speechRequest = UnityWebRequest.GetRequestUri(new Uri(textToSpeechRequestBody)))
        {
            // 将DownloadHandlerAudioClip改为DownloadHandlerBuffer
            speechRequest.downloadHandler = new DownloadHandlerBuffer();

            yield return speechRequest.SendWebRequest();

            if (speechRequest.result == UnityWebRequest.Result.Success)
            {
                // 获取返回的数据
                string responseText = speechRequest.downloadHandler.text;
                // 解析返回的数据(根据实际返回的数据格式进行解析)
                // ...

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

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

    // 省略GetVoice方法和GenerateTextToSpeech方法
}

请注意,以上代码只是示例,具体的回调函数的参数名和返回数据的解析方式需要根据实际情况进行修改。

将XHR类型改为JSONP类型发送完整示例一下修改后的代码using System;using SystemCollections;using SystemXmlLinq;using UnityEngine;using UnityEngineNetworking;public class GenshinTextToSpeech TTS #region 参数定义 summary

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

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