Для начала я просто хочу создать небольшую сцену с полем ввода, куда вы можете ввести текст, который затем будет выведен после того, как вы нажмете Enter. Я спроектировал все согласно тестовому примеру из Watson Unity SDK https://github.com/watson-developer-cloud/unity-sdk.
Я построил эту сцену:
Для начала я просто хочу создать небольшую сцену с полем ввода, куда вы можете ввести текст, который затем будет выведен после того, как вы нажмете Enter. Я спроектировал все согласно тестовому примеру из Watson Unity SDK https://github.com/watson-developer-cloud/unity-sdk. Я построил эту сцену: [img]https://i.stack.imgur.com/bmegY.png[/img]
И написал этот код: [code]using IBM.Watson.TextToSpeech.V1; using IBM.Watson.TextToSpeech.V1.Model; using IBM.Cloud.SDK.Utilities; using IBM.Cloud.SDK.Authentication; using IBM.Cloud.SDK.Authentication.Iam; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using IBM.Cloud.SDK;
namespace IBM.Watson.Examples { public class ExampleTextToSpeechV1 : MonoBehaviour { #region PLEASE SET THESE VARIABLES IN THE INSPECTOR [Space(10)] [Tooltip("The IAM apikey.")] [SerializeField] private string iamApikey = "APIKEY"; [Tooltip("The service URL (optional). This defaults to \"https://api.us-south.text-to-speech.watson.cloud.ibm.com\"")] [SerializeField] private string serviceUrl = "https://api.eu-de.text-to-speech.watson.cloud.ibm.com/instances/SERVICEINSTANCE"; private TextToSpeechService service; private string allisionVoice = "en-US_AllisonV3Voice"; private string synthesizeText = "Hello, my Name is Niclas."; private string placeholderText = "Please type text here and press enter."; private string waitingText = "Watson Text to Speech service is synthesizing the audio!"; private string synthesizeMimeType = "audio/wav"; public InputField textInput; private bool _textEntered = false; private AudioClip _recording = null; private byte[] audioStream = null; #endregion
void Update() { if (Input.GetKeyDown(KeyCode.Return)) { Runnable.Run(ExampleSynthesize(textInput.text)); } }
private IEnumerator CreateService() { if (string.IsNullOrEmpty(iamApikey)) { throw new IBMException("Please add IAM ApiKey to the Iam Apikey field in the inspector."); }
IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey);
while (!authenticator.CanAuthenticate()) { yield return null; }
service = new TextToSpeechService(authenticator); if (!string.IsNullOrEmpty(serviceUrl)) { service.SetServiceUrl(serviceUrl); } }
#region Synthesize Example private IEnumerator ExampleSynthesize(string text) { if (string.IsNullOrEmpty(text)) { text = synthesizeText; Log.Debug("ExampleTextToSpeechV1", "Using default text, please enter your own text in dialog box!");