我如何使用Unity Web Requests流音频...我想使用基于云的TTS [英] How do I use Unity Web Requests for streaming audio... I want to use a cloud based TTS

查看:179
本文介绍了我如何使用Unity Web Requests流音频...我想使用基于云的TTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Unity游戏中使用云TTS.

I'm trying to use a cloud TTS within my Unity game.

对于较新的版本(我正在使用2019.1),他们不赞成使用WWW,而希望使用API​​中的UnityWebRequest.

With the newer versions (I am using 2019.1), they have deprecated WWW in favour of UnityWebRequest(s) within the API.

我已经尝试过 Unity文档,但是没有这样做.为我工作.

I have tried the Unity Documentation, but that didn't work for me.

我还尝试了其他线程,它们使用的WWW在我的Unity版本中已弃用.

I have also tried other threads and they use WWW which is deprecated for my Unity version.

    void Start()
    {
        StartCoroutine(PlayTTS());
    }

    IEnumerator PlayTTS()
    {
        using (UnityWebRequestMultimedia wr = new UnityWebRequestMultimedia.GetAudioClip(
            "https://example.com/tts?text=Sample%20Text&voice=Male",
            AudioType.OGGVORBIS)
        )

        {
            yield return wr.Send();

            if (wr.isNetworkError)
            {
                Debug.LogWarning(wr.error);
            }

            else
            {
                //AudioClip ttsClip = DownloadHandlerAudioClip.GetContent(wr);
            }
        }
    }

浏览器(我使用Firefox)中的URL成功加载了音频剪辑,使我可以播放它.

The URL in a browser (I used Firefox) successfully loaded the audio clip allowing me to play it.

我想要做的是在游戏中发生某些事情时播放TTS,出于测试目的,它已经在"void Start"内完成了.

What I want it to do is play the TTS when something happens in the game, it has been done within the "void Start" for testing purposes.

我要去哪里错了?

先谢谢了
乔什

Thanks in advance
Josh

推荐答案

> c1> 自动添加默认的 DownloadHandlerAudioClip 属性 streamAudio .

UnityWebRequestMultimedia.GetAudioClip automatically adds a default DownloadHandlerAudioClip which has a property streamAudio.

将此设置为true并添加 UnityWebRequest.downloadedBytes ,以便在开始播放前延迟播放.

Set this to true and add a check for UnityWebRequest.downloadedBytes in order to delay the playback before starting.

类似

public AudioSource source;

IEnumerator PlayTTS()
{
    using (var wr = new UnityWebRequestMultimedia.GetAudioClip(
        "https://example.com/tts?text=Sample%20Text&voice=Male",
        AudioType.OGGVORBIS)
    )

    {
        ((DownloadHandlerAudioClip)wr.downloadHandler).streamAudio = true;

        wr.Send();

        while(!wr.isNetworkError && wr.downloadedBytes <= someThreshold)
        {
            yield return null;
        }

        if (wr.isNetworkError)
        {
            Debug.LogWarning(wr.error);
        }

        else
        {
            // Here I'm not sure if you would use
            source.PlayOneShot(DownloadHandlerAudioClip.GetContent(wr)); 
            // or rather
            source.PlayOneShot((DownloadHandlerAudioClip)wr.downloadHandler).audioClip);     
        }
    }
}


在智能手机上键入内容,因此没有保修,但希望您能理解

这篇关于我如何使用Unity Web Requests流音频...我想使用基于云的TTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆