Unity
[Unity] TTS(Text to Speech) 기능 코드 구현 방법
JiHxxn
2024. 5. 28. 17:30
public class Button_TTS : MonoBehaviour
{
private Button button_TTS;
private AudioSource audioSource;
private string tts_ment;
public string TTS_Ment { get { return tts_ment; } set { tts_ment = value; } }
private void Start()
{
button_TTS = GetComponent();
audioSource = GetComponent();
button_TTS.onClick.RemoveAllListeners();
button_TTS.onClick.AddListener(OnClickButton_TTS);
}
public void StopAudio()
{
audioSource.Stop();
}
#region TTS
public void OnClickButton_TTS()
{
string url = "<https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=>";
string str = string.Format("{0}{1}&tl=ko-KR", url, tts_ment);
Debug.Log(str);
StopAllCoroutines();
StartCoroutine(PlaySpeak(str));
}
IEnumerator PlaySpeak(string str)
{
WWW www = new WWW(str);
yield return www;
audioSource.clip = www.GetAudioClip(false, true, AudioType.MPEG);
audioSource.Play();
}
#endregion
}
- 테스트 해보니 글자 수 제한이 있는 듯 하다.